Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ pub enum SettingsTab {
Commands,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HotkeyTarget {
Toggle,
Clipboard,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HotkeyCapture {
Idle,
Recording {
target: HotkeyTarget,
candidate: Option<Shortcut>,
},
}

impl HotkeyCapture {
pub fn is_recording(&self) -> bool {
matches!(self, Self::Recording { .. })
}
}

/// Actions that open a native file dialog
#[derive(Debug, Clone)]
pub enum FileDialogAction {
Expand All @@ -65,22 +86,10 @@ pub enum ResetField {
Placeholder,
SearchUrl,
DebounceDelay,
StartAtLogin,
AutoUpdate,
HapticFeedback,
ShowMenubarIcon,
ClipboardHistory,
ClipboardPasteOnSelect,
MainPage,
ShowScrollbar,
ClearOnHide,
ClearOnEnter,
ShowIcons,
Font,
EventDuration,
TextColor,
BackgroundColor,
ThemeMode,
Aliases,
Modes,
SearchDirs,
Expand Down Expand Up @@ -169,14 +178,22 @@ pub enum Message {
SimulatePaste(i32),
OpenSettingsWindow,
SettingsWindowOpened(window::Id),
KeyboardEvent {
event: iced::Event,
window: window::Id,
},
BeginHotkeyCapture(HotkeyTarget),
HotkeyCaptureKeyPressed {
physical_key: iced::keyboard::key::Physical,
modifiers: iced::keyboard::Modifiers,
},
FinishHotkeyCapture,
}

#[derive(Debug, Clone)]
#[allow(unused)]
pub enum SetConfigFields {
ToDefault,
ToggleHotkey(String),
ClipboardHotkey(String),
SetPosition(Position),
PlaceHolder(String),
SearchUrl(String),
Expand Down
101 changes: 79 additions & 22 deletions src/app/pages/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ use crate::app::FileDialogAction;
use crate::app::ResetField;
use crate::app::SetConfigBufferFields;
use crate::app::SetConfigThemeFields;
use crate::app::SettingsTab;
use crate::app::{HotkeyCapture, HotkeyTarget, SettingsTab};
use crate::commands::Function;
use crate::config::MainPage;
use crate::config::Shelly;
use crate::config::ThemeMode;
use crate::platform::macos::launching::Shortcut;
use crate::styles::delete_button_style;
use crate::styles::settings_add_button_style;
use crate::styles::settings_radio_button_style;
Expand All @@ -53,7 +54,11 @@ const SETTINGS_ITEM_HEIGHT: u32 = 55;
const SETTINGS_ITEM_COL_SPACING: u32 = 3;
const SETTINGS_INPUT_WIDTH: f32 = 250.0;

pub fn settings_page(config: Config, settings_tab: SettingsTab) -> Element<'static, Message> {
pub fn settings_page(
config: Config,
settings_tab: SettingsTab,
hotkey_capture: HotkeyCapture,
) -> Element<'static, Message> {
let config = Box::new(config.clone());
let theme = config.theme.clone();

Expand Down Expand Up @@ -83,7 +88,7 @@ pub fn settings_page(config: Config, settings_tab: SettingsTab) -> Element<'stat
.align_x(Alignment::Center);

let tab_content: Column<'static, Message> = match settings_tab {
SettingsTab::General => general_tab(config.clone(), theme.clone()),
SettingsTab::General => general_tab(config.clone(), theme.clone(), hotkey_capture),
SettingsTab::Appearance => appearance_tab(config.clone(), theme.clone()),
SettingsTab::Commands => commands_tab(config.clone(), theme.clone()),
};
Expand Down Expand Up @@ -166,44 +171,44 @@ fn reset_button(theme: crate::config::Theme, field: ResetField) -> Element<'stat
.into()
}

fn general_tab(config: Box<Config>, theme: crate::config::Theme) -> Column<'static, Message> {
let theme_clone = theme.clone();
fn general_tab(
config: Box<Config>,
theme: crate::config::Theme,
hotkey_capture: HotkeyCapture,
) -> Column<'static, Message> {
let hotkey = settings_row_with_reset(
settings_item_row([
settings_hint_text(
theme.clone(),
"Toggle hotkey",
Some("Use \"+\" as a seperator"),
Some("Click the field to record and ESC once done"),
),
Space::new().width(Length::Fill).into(),
text_input("Toggle Hotkey", &config.toggle_hotkey)
.on_input(|input| Message::SetConfig(SetConfigFields::ToggleHotkey(input.clone())))
.on_submit(Message::WriteConfig)
.width(Length::Fixed(SETTINGS_INPUT_WIDTH))
.style(move |_, _| settings_text_input_item_style(&theme_clone))
.into(),
hotkey_field(
&config.toggle_hotkey,
HotkeyTarget::Toggle,
&hotkey_capture,
theme.clone(),
),
]),
ResetField::ToggleHotkey,
theme.clone(),
);

let theme_clone = theme.clone();
let cb_hotkey = settings_row_with_reset(
settings_item_row([
settings_hint_text(
theme.clone(),
"Clipboard hotkey",
Some("Use \"+\" as a seperator"),
Some("Click the field to record and ESC once done"),
),
Space::new().width(Length::Fill).into(),
text_input("Clipboard Hotkey", &config.clipboard_hotkey)
.on_input(|input| {
Message::SetConfig(SetConfigFields::ClipboardHotkey(input.clone()))
})
.on_submit(Message::WriteConfig)
.width(Length::Fixed(SETTINGS_INPUT_WIDTH))
.style(move |_, _| settings_text_input_item_style(&theme_clone))
.into(),
hotkey_field(
&config.clipboard_hotkey,
HotkeyTarget::Clipboard,
&hotkey_capture,
theme.clone(),
),
]),
ResetField::ClipboardHotkey,
theme.clone(),
Expand Down Expand Up @@ -478,6 +483,58 @@ fn general_tab(config: Box<Config>, theme: crate::config::Theme) -> Column<'stat
.spacing(10)
}

fn hotkey_field(
value: &str,
target: HotkeyTarget,
capture: &HotkeyCapture,
theme: crate::config::Theme,
) -> Element<'static, Message> {
let (label, recording) = match capture {
HotkeyCapture::Recording {
target: active_target,
candidate,
} if *active_target == target => (
candidate
.as_ref()
.map(Shortcut::display_string)
.unwrap_or_else(|| "Press a shortcut…".to_string()),
true,
),
_ => (
Shortcut::parse(value)
.map(|shortcut| shortcut.display_string())
.unwrap_or_else(|_| value.to_string()),
false,
),
};
let theme_clone = theme.clone();

Button::new(
Text::new(label)
.font(theme.font())
.align_y(Alignment::Center)
.width(Length::Fill),
)
.width(Length::Fixed(SETTINGS_INPUT_WIDTH))
.height(36)
.padding([0, 12])
.style(move |_, _| button::Style {
text_color: theme_clone.text_color(1.0),
background: Some(Background::Color(with_alpha(
tint(theme_clone.bg_color(), if recording { 0.16 } else { 0.08 }),
0.9,
))),
border: Border {
color: theme_clone.text_color(if recording { 0.8 } else { 0.3 }),
width: if recording { 1.0 } else { 0.2 },
radius: Radius::new(10),
},
..Default::default()
})
.on_press(Message::BeginHotkeyCapture(target))
.into()
}

fn appearance_tab(config: Box<Config>, theme: crate::config::Theme) -> Column<'static, Message> {
let theme_clone = theme.clone();
let theme_mode_setting = settings_row_without_reset(settings_item_row([
Expand Down
75 changes: 45 additions & 30 deletions src/app/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod elm;
pub mod update;

use crate::app::apps::App;
use crate::app::{ArrowKey, Message, Move, Page};
use crate::app::{ArrowKey, HotkeyCapture, Message, Move, Page};
use crate::autoupdate::new_version_available;
use crate::clipboard::ClipBoardContentType;
use crate::config::{Config, Shelly};
Expand Down Expand Up @@ -205,6 +205,7 @@ pub struct Tile {
pub settings_tab: crate::app::SettingsTab,
debouncer: Debouncer,
pub settings_window: Option<window::Id>,
pub hotkey_capture: HotkeyCapture,
previous_input_source: Option<String>,
}

Expand Down Expand Up @@ -278,34 +279,34 @@ impl Tile {
/// - Keypresses (escape to close the window)
/// - Window focus changes
pub fn subscription(&self) -> Subscription<Message> {
let keyboard = event::listen_with(|event, _, id| match event {
iced::Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Named(keyboard::key::Named::Escape),
..
}) => Some(Message::EscKeyPressed(id)),
iced::Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Character(cha),
modifiers: Modifiers::LOGO,
..
}) => {
if cha.to_string() == "," {
return Some(Message::OpenSettingsWindow);
}
None
}
_ => None,
});
Subscription::batch([
Subscription::run(handle_hot_reloading),
keyboard,
Subscription::run(crate::platform::macos::urlscheme::url_stream),
Subscription::run(handle_recipient),
Subscription::run(reload_events),
Subscription::run(handle_version_and_rankings),
Subscription::run(handle_theme_mode),
Subscription::run(handle_clipboard_history),
Subscription::run(handle_file_search),
window::close_events().map(Message::HideWindow),
let is_recording_hotkey = self.hotkey_capture.is_recording();
let capture_events = if is_recording_hotkey {
event::listen_with(|event, _, window| match event {
iced::Event::Keyboard(_) => Some(Message::KeyboardEvent { event, window }),
_ => None,
})
} else {
Subscription::none()
};
let window_keyboard = if is_recording_hotkey {
Subscription::none()
} else {
event::listen_with(|event, _, id| match event {
iced::Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Named(keyboard::key::Named::Escape),
..
}) => Some(Message::EscKeyPressed(id)),
iced::Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Character(cha),
modifiers: Modifiers::LOGO,
..
}) if cha == "," => Some(Message::OpenSettingsWindow),
_ => None,
})
};
let keyboard = if is_recording_hotkey {
Subscription::none()
} else {
keyboard::listen().filter_map(|event| {
if let keyboard::Event::KeyPressed { key, modifiers, .. } = event {
match key {
Expand Down Expand Up @@ -347,7 +348,21 @@ impl Tile {
} else {
None
}
}),
})
};
Subscription::batch([
Subscription::run(handle_hot_reloading),
capture_events,
window_keyboard,
Subscription::run(crate::platform::macos::urlscheme::url_stream),
Subscription::run(handle_recipient),
Subscription::run(reload_events),
Subscription::run(handle_version_and_rankings),
Subscription::run(handle_theme_mode),
Subscription::run(handle_clipboard_history),
Subscription::run(handle_file_search),
window::close_events().map(Message::HideWindow),
keyboard,
window::events()
.with(self.focused)
.filter_map(|(focused, (wid, event))| match event {
Expand Down
9 changes: 7 additions & 2 deletions src/app/tile/elm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rayon::slice::ParallelSliceMut;
use crate::app::pages::emoji::emoji_page;
use crate::app::pages::settings::settings_page;
use crate::app::tile::{AppIndex, Hotkeys};
use crate::app::{DEFAULT_WINDOW_HEIGHT, SettingsTab, ToApp, ToApps};
use crate::app::{DEFAULT_WINDOW_HEIGHT, HotkeyCapture, SettingsTab, ToApp, ToApps};
use crate::config::Theme;
use crate::debounce::Debouncer;
use crate::platform;
Expand Down Expand Up @@ -106,6 +106,7 @@ pub fn new(hotkeys: Hotkeys, config: &Config) -> (Tile, Task<Message>) {
settings_tab: SettingsTab::General,
debouncer: Debouncer::new(config.debounce_delay),
settings_window: None,
hotkey_capture: HotkeyCapture::Idle,
previous_input_source: None,
},
Task::batch([open.map(|_| Message::OpenWindow)]),
Expand All @@ -115,7 +116,11 @@ pub fn new(hotkeys: Hotkeys, config: &Config) -> (Tile, Task<Message>) {
/// The elm View function that renders the entire rustcast window
pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
if tile.settings_window == Some(wid) {
return settings_page(tile.config.clone(), tile.settings_tab);
return settings_page(
tile.config.clone(),
tile.settings_tab,
tile.hotkey_capture.clone(),
);
};

if tile.visible {
Expand Down
Loading
Loading