Skip to content

Commit

Permalink
make all actions unified
Browse files Browse the repository at this point in the history
  • Loading branch information
LegitCamper committed Sep 21, 2024
1 parent 0bd6109 commit 9c1964a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 58 deletions.
5 changes: 3 additions & 2 deletions examples/keyboard_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use iced_aw::BOOTSTRAP_FONT_BYTES;
use std::time::Duration;

use icy_browser::{
widgets, BrowserWidget, KeyType, ShortcutBuilder, ShortcutModifier, ShortcutType, Ultralight,
widgets, BrowserWidget, KeyType, Message as WidgetMessage, ShortcutBuilder, ShortcutModifier,
Ultralight,
};

fn main() -> iced::Result {
Expand Down Expand Up @@ -40,7 +41,7 @@ impl Default for Browser {
fn default() -> Self {
let shortcuts = ShortcutBuilder::new()
.add_shortcut(
ShortcutType::ToggleOverlay,
WidgetMessage::ToggleOverlay,
vec![
KeyType::Modifier(ShortcutModifier::Ctrl),
KeyType::Key(iced::keyboard::Key::Character("e".into())),
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub use engines::{BrowserEngine, PixelFormat, Tab, TabInfo, Tabs};
pub use engines::ultralight::Ultralight;

pub mod widgets;
pub use widgets::{nav_bar, tab_bar, BrowserWidget};
pub use widgets::{nav_bar, tab_bar, BrowserWidget, Message};

mod shortcut;
pub use shortcut::{KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, ShortcutType, Shortcuts};
pub use shortcut::{KeyType, Shortcut, ShortcutBuilder, ShortcutModifier, Shortcuts};

// Image details for passing the view around
#[derive(Debug, Clone)]
Expand Down
23 changes: 4 additions & 19 deletions src/shortcut.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use iced::keyboard::{Key, Modifiers};

use super::widgets::Message;

pub struct ShortcutBuilder(Shortcuts);
impl ShortcutBuilder {
pub fn new() -> Self {
ShortcutBuilder(Vec::new())
}

pub fn add_shortcut(
mut self,
shortcut_action: ShortcutType,
shortcut_keys: Vec<KeyType>,
) -> Self {
pub fn add_shortcut(mut self, shortcut_action: Message, shortcut_keys: Vec<KeyType>) -> Self {
if self.0.iter().filter(|sc| sc.0 == shortcut_action).count() != 0 {
panic!("Tried to add a duplicated shortcut");
}
Expand Down Expand Up @@ -73,24 +71,11 @@ pub enum KeyType {
Modifier(ShortcutModifier),
}
/// Configures Widget Keyboard Shortcut
pub type Shortcut = (ShortcutType, Vec<KeyType>);
pub type Shortcut = (Message, Vec<KeyType>);

/// Configures Widget Keyboard Shortcuts
pub type Shortcuts = Vec<Shortcut>;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ShortcutType {
GoBackward,
GoForward,
Refresh,
GoHome,
CloseCurrentTab,
CreateTab,
ToggleOverlay,
ShowOverlay,
HideOverlay,
}

pub fn check_shortcut(shortcut: &Shortcut, key: &Key, modifiers: &Modifiers) -> bool {
shortcut
.1
Expand Down
48 changes: 13 additions & 35 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use command_window::command_window;

use crate::{engines::BrowserEngine, shortcut::check_shortcut, to_url, ImageInfo, Shortcuts};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Message {
GoBackward,
GoForward,
Expand All @@ -36,12 +36,13 @@ pub enum Message {
SendMouseEvent(Point, mouse::Event),
UpdateViewSize(Size<u32>),
Event(Event),
ToggleOverlay,
ShowOverlay,
HideOverlay,
}

/// Allows different widgets to interact in their native way
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum TabSelectionType {
Id(u32),
Index(usize),
Expand Down Expand Up @@ -286,6 +287,15 @@ where
self.query = query;
Task::none()
}
Message::ToggleOverlay => {
if self.show_overlay {
self.show_overlay = false;
widget::focus_next()
} else {
self.show_overlay = true;
widget::focus_next()
}
}
Message::ShowOverlay => {
self.show_overlay = true;
widget::focus_next()
Expand Down Expand Up @@ -315,39 +325,7 @@ where
// Shortcut (Customizable) behaviors
for shortcut in self.shortcuts.iter() {
if check_shortcut(shortcut, &key, &modifiers) {
match shortcut.0 {
crate::ShortcutType::GoBackward => {
return Task::done(Message::GoBackward)
}
crate::ShortcutType::GoForward => {
return Task::done(Message::GoForward)
}
crate::ShortcutType::Refresh => {
return Task::done(Message::Refresh)
}
crate::ShortcutType::GoHome => {
return Task::done(Message::GoHome)
}
crate::ShortcutType::CloseCurrentTab => {
return Task::done(Message::CloseCurrentTab)
}
crate::ShortcutType::CreateTab => {
return Task::done(Message::CreateTab)
}
crate::ShortcutType::ToggleOverlay => {
if self.show_overlay {
return Task::done(Message::HideOverlay);
} else {
return Task::done(Message::ShowOverlay);
}
}
crate::ShortcutType::ShowOverlay => {
return Task::done(Message::ShowOverlay)
}
crate::ShortcutType::HideOverlay => {
return Task::done(Message::HideOverlay)
}
}
return Task::done(shortcut.0.clone());
}
}
}
Expand Down

0 comments on commit 9c1964a

Please sign in to comment.