Added shortcut settings and enabled customizing shortcuts
This commit is contained in:
1 parent
1f5da251dc
commit
f720fa00f4
2 files changed
+623
-80
No files matched your search
+591
-71
@@ -1,5 +1,5 @@
|
||||
use iced::widget::{
|
||||
button, column, container, mouse_area, pick_list, row, rule, scrollable, space, stack, text,
|
||||
button, column, container, mouse_area, row, rule, scrollable, space, stack, text, text_input,
|
||||
tooltip,
|
||||
};
|
||||
use iced::widget::{Id, operation};
|
||||
@@ -18,8 +18,6 @@ const MAX_TAB_TITLE: usize = 20;
|
||||
const MENU_ITEM_HEIGHT: f32 = 24.0;
|
||||
/// Fixed height of the hamburger row inside the menu panel.
|
||||
const HAMBURGER_ROW_HEIGHT: f32 = 24.0;
|
||||
/// Fixed height of the theme picker field inside the menu panel.
|
||||
const THEME_FIELD_HEIGHT: f32 = 40.0;
|
||||
/// Panel padding of the hamburger menu.
|
||||
const MENU_PADDING: f32 = 4.0;
|
||||
/// Pixels moved per press of a tab-strip scroll button.
|
||||
@@ -35,47 +33,253 @@ fn main() -> iced::Result {
|
||||
.run()
|
||||
}
|
||||
|
||||
fn subscription(_state: &Moorly) -> Subscription<Message> {
|
||||
keyboard::listen().filter_map(|event| match event {
|
||||
keyboard::Event::KeyPressed {
|
||||
key: keyboard::Key::Character(key),
|
||||
modifiers,
|
||||
repeat: false,
|
||||
..
|
||||
} if key.as_str() == "," && modifiers.control() => {
|
||||
Some(Message::SelectTab(Tab::Settings))
|
||||
fn subscription(state: &Moorly) -> Subscription<Message> {
|
||||
keyboard::listen()
|
||||
.with((
|
||||
state.shortcuts.settings.clone(),
|
||||
state.shortcuts.quit.clone(),
|
||||
state.shortcut_capture.is_some(),
|
||||
))
|
||||
.filter_map(|((settings_shortcut, quit_shortcut, capturing), event)| {
|
||||
let keyboard::Event::KeyPressed {
|
||||
key,
|
||||
modifiers,
|
||||
repeat: false,
|
||||
..
|
||||
} = event
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if capturing {
|
||||
return Some(Message::CaptureKey { key, modifiers });
|
||||
}
|
||||
|
||||
if quit_shortcut
|
||||
.as_ref()
|
||||
.is_some_and(|shortcut| shortcut.matches(&key, modifiers))
|
||||
{
|
||||
return Some(Message::Quit);
|
||||
}
|
||||
|
||||
if let Some(shortcut) = &settings_shortcut {
|
||||
if shortcut.matches(&key, modifiers) {
|
||||
return Some(Message::SelectTab(Tab::Settings));
|
||||
}
|
||||
}
|
||||
|
||||
match key {
|
||||
keyboard::Key::Character(key)
|
||||
if cfg!(debug_assertions)
|
||||
&& key.eq_ignore_ascii_case("t")
|
||||
&& modifiers.control()
|
||||
&& modifiers.shift() =>
|
||||
{
|
||||
Some(Message::AddTab)
|
||||
}
|
||||
keyboard::Key::Character(key)
|
||||
if key.eq_ignore_ascii_case("w")
|
||||
&& modifiers.control()
|
||||
&& modifiers.shift() =>
|
||||
{
|
||||
Some(Message::CloseActiveTab)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct Shortcut {
|
||||
control: bool,
|
||||
shift: bool,
|
||||
alt: bool,
|
||||
key: String,
|
||||
}
|
||||
|
||||
impl Shortcut {
|
||||
/// Parses text like `Ctrl+Shift+K` into a shortcut. Unknown modifiers and
|
||||
/// missing keys are rejected.
|
||||
fn parse(input: &str) -> Option<Self> {
|
||||
let parts: Vec<&str> = input
|
||||
.split('+')
|
||||
.map(str::trim)
|
||||
.filter(|part| !part.is_empty())
|
||||
.collect();
|
||||
let (key, modifiers) = parts.split_last()?;
|
||||
|
||||
// Only single-character keys are supported for now.
|
||||
if key.chars().count() != 1 {
|
||||
return None;
|
||||
}
|
||||
keyboard::Event::KeyPressed {
|
||||
key: keyboard::Key::Character(key),
|
||||
modifiers,
|
||||
repeat: false,
|
||||
..
|
||||
} if cfg!(debug_assertions)
|
||||
&& key.eq_ignore_ascii_case("t")
|
||||
&& modifiers.control()
|
||||
&& modifiers.shift() =>
|
||||
|
||||
let mut shortcut = Self {
|
||||
control: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
key: key.to_ascii_lowercase(),
|
||||
};
|
||||
|
||||
for modifier in modifiers {
|
||||
match modifier.to_ascii_lowercase().as_str() {
|
||||
"ctrl" | "control" => shortcut.control = true,
|
||||
"shift" => shortcut.shift = true,
|
||||
"alt" => shortcut.alt = true,
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
|
||||
Some(shortcut)
|
||||
}
|
||||
|
||||
fn matches(&self, key: &keyboard::Key, modifiers: keyboard::Modifiers) -> bool {
|
||||
let keyboard::Key::Character(key) = key else {
|
||||
return false;
|
||||
};
|
||||
|
||||
self.control == modifiers.control()
|
||||
&& self.shift == modifiers.shift()
|
||||
&& self.alt == modifiers.alt()
|
||||
&& key.as_str().eq_ignore_ascii_case(&self.key)
|
||||
}
|
||||
|
||||
/// Builds a shortcut from a captured key press. Plain keys are rejected so
|
||||
/// a shortcut cannot hijack ordinary typing.
|
||||
fn from_key(key: &keyboard::Key, modifiers: keyboard::Modifiers) -> Option<Self> {
|
||||
let keyboard::Key::Character(key) = key else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if !(modifiers.control() || modifiers.alt()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Self {
|
||||
control: modifiers.control(),
|
||||
shift: modifiers.shift(),
|
||||
alt: modifiers.alt(),
|
||||
key: key.as_str().to_ascii_lowercase(),
|
||||
})
|
||||
}
|
||||
|
||||
fn key_label(&self) -> String {
|
||||
let is_letter = self.key.chars().count() == 1
|
||||
&& self.key.chars().next().is_some_and(char::is_alphabetic);
|
||||
|
||||
if is_letter {
|
||||
self.key.to_ascii_uppercase()
|
||||
} else {
|
||||
self.key.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Shortcut {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut parts = Vec::new();
|
||||
if self.control {
|
||||
parts.push("Ctrl".to_owned());
|
||||
}
|
||||
if self.shift {
|
||||
parts.push("Shift".to_owned());
|
||||
}
|
||||
if self.alt {
|
||||
parts.push("Alt".to_owned());
|
||||
}
|
||||
parts.push(self.key_label());
|
||||
|
||||
f.write_str(&parts.join("+"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum ShortcutAction {
|
||||
Settings,
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl ShortcutAction {
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::Settings => "Settings",
|
||||
Self::Quit => "Quit",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Shortcuts {
|
||||
settings: Option<Shortcut>,
|
||||
quit: Option<Shortcut>,
|
||||
}
|
||||
|
||||
impl Default for Shortcuts {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
settings: Shortcut::parse("Ctrl+,"),
|
||||
quit: Shortcut::parse("Ctrl+Shift+Q"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Shortcuts {
|
||||
fn get(&self, action: ShortcutAction) -> Option<&Shortcut> {
|
||||
match action {
|
||||
ShortcutAction::Settings => self.settings.as_ref(),
|
||||
ShortcutAction::Quit => self.quit.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
fn set(&mut self, action: ShortcutAction, shortcut: Option<Shortcut>) {
|
||||
match action {
|
||||
ShortcutAction::Settings => self.settings = shortcut,
|
||||
ShortcutAction::Quit => self.quit = shortcut,
|
||||
}
|
||||
}
|
||||
|
||||
/// Name of another action already bound to `candidate`, if any. `except`
|
||||
/// is the action being rebound, which must not conflict with itself.
|
||||
fn conflict(&self, candidate: &Shortcut, except: ShortcutAction) -> Option<&'static str> {
|
||||
let configurable = [
|
||||
(ShortcutAction::Settings, &self.settings),
|
||||
(ShortcutAction::Quit, &self.quit),
|
||||
];
|
||||
if let Some((action, _)) = configurable
|
||||
.iter()
|
||||
.find(|(action, shortcut)| {
|
||||
*action != except && shortcut.as_ref() == Some(candidate)
|
||||
})
|
||||
{
|
||||
Some(Message::AddTab)
|
||||
return Some(action.label());
|
||||
}
|
||||
keyboard::Event::KeyPressed {
|
||||
key: keyboard::Key::Character(key),
|
||||
modifiers,
|
||||
repeat: false,
|
||||
..
|
||||
} if key.eq_ignore_ascii_case("w")
|
||||
&& modifiers.control()
|
||||
&& modifiers.shift() =>
|
||||
{
|
||||
Some(Message::CloseActiveTab)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
|
||||
// Shortcuts that are fixed for now but still own their keys.
|
||||
let reserved: &[(&str, &str)] = if cfg!(debug_assertions) {
|
||||
&[("Close Active Tab", "Ctrl+Shift+W"), ("Add Tab", "Ctrl+Shift+T")]
|
||||
} else {
|
||||
&[("Close Active Tab", "Ctrl+Shift+W")]
|
||||
};
|
||||
|
||||
reserved.iter().find_map(|(name, text)| {
|
||||
(Shortcut::parse(text).as_ref() == Some(candidate)).then_some(*name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Keys picked so far while rebinding a shortcut.
|
||||
#[derive(Debug, Clone)]
|
||||
struct ShortcutCapture {
|
||||
action: ShortcutAction,
|
||||
candidate: Option<Shortcut>,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
struct Moorly {
|
||||
menu_open: bool,
|
||||
tabs_submenu_open: bool,
|
||||
theme: theme::Preference,
|
||||
shortcuts: Shortcuts,
|
||||
shortcut_capture: Option<ShortcutCapture>,
|
||||
tabs: Vec<Tab>,
|
||||
active_tab: Option<Tab>,
|
||||
settings_section: SettingsSection,
|
||||
@@ -106,17 +310,25 @@ enum SettingsSection {
|
||||
General,
|
||||
Startup,
|
||||
Appearance,
|
||||
Shortcuts,
|
||||
Profiles,
|
||||
}
|
||||
|
||||
impl SettingsSection {
|
||||
const ALL: [Self; 4] = [Self::General, Self::Startup, Self::Appearance, Self::Profiles];
|
||||
const ALL: [Self; 5] = [
|
||||
Self::General,
|
||||
Self::Startup,
|
||||
Self::Appearance,
|
||||
Self::Shortcuts,
|
||||
Self::Profiles,
|
||||
];
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::General => "General",
|
||||
Self::Startup => "Startup",
|
||||
Self::Appearance => "Appearance",
|
||||
Self::Shortcuts => "Shortcuts",
|
||||
Self::Profiles => "Profiles",
|
||||
}
|
||||
}
|
||||
@@ -128,6 +340,8 @@ impl Default for Moorly {
|
||||
menu_open: false,
|
||||
tabs_submenu_open: false,
|
||||
theme: theme::load(),
|
||||
shortcuts: Shortcuts::default(),
|
||||
shortcut_capture: None,
|
||||
tabs: Vec::new(),
|
||||
active_tab: None,
|
||||
settings_section: SettingsSection::default(),
|
||||
@@ -151,6 +365,14 @@ enum Message {
|
||||
SelectTab(Tab),
|
||||
CloseTab(Tab),
|
||||
SelectSettingsSection(SettingsSection),
|
||||
OpenShortcutCapture(ShortcutAction),
|
||||
CancelShortcut,
|
||||
ClearShortcut(ShortcutAction),
|
||||
ApplyShortcut,
|
||||
CaptureKey {
|
||||
key: keyboard::Key,
|
||||
modifiers: keyboard::Modifiers,
|
||||
},
|
||||
TabsScrolled(scrollable::Viewport),
|
||||
ScrollTabs(mouse::ScrollDelta),
|
||||
ScrollTabsBy(f32),
|
||||
@@ -202,8 +424,6 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
}
|
||||
Message::SetTheme(preference) => {
|
||||
state.theme = preference;
|
||||
state.menu_open = false;
|
||||
state.tabs_submenu_open = false;
|
||||
theme::save(preference);
|
||||
}
|
||||
Message::SelectTab(tab) => {
|
||||
@@ -226,6 +446,44 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
}
|
||||
}
|
||||
Message::SelectSettingsSection(section) => state.settings_section = section,
|
||||
Message::OpenShortcutCapture(action) => {
|
||||
state.shortcut_capture = Some(ShortcutCapture {
|
||||
action,
|
||||
candidate: None,
|
||||
error: None,
|
||||
});
|
||||
}
|
||||
Message::CancelShortcut => state.shortcut_capture = None,
|
||||
Message::ClearShortcut(action) => {
|
||||
state.shortcuts.set(action, None);
|
||||
state.shortcut_capture = None;
|
||||
}
|
||||
Message::ApplyShortcut => apply_shortcut(state),
|
||||
Message::CaptureKey { key, modifiers } => match key {
|
||||
keyboard::Key::Named(keyboard::key::Named::Escape) => {
|
||||
state.shortcut_capture = None;
|
||||
}
|
||||
keyboard::Key::Named(keyboard::key::Named::Enter) => apply_shortcut(state),
|
||||
keyboard::Key::Named(
|
||||
keyboard::key::Named::Control
|
||||
| keyboard::key::Named::Shift
|
||||
| keyboard::key::Named::Alt,
|
||||
) => {}
|
||||
_ => {
|
||||
if let Some(capture) = state.shortcut_capture.as_mut() {
|
||||
match Shortcut::from_key(&key, modifiers) {
|
||||
Some(shortcut) => {
|
||||
capture.candidate = Some(shortcut);
|
||||
capture.error = None;
|
||||
}
|
||||
None => {
|
||||
capture.candidate = None;
|
||||
capture.error = Some("Use Ctrl or Alt plus a key.".to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Message::TabsScrolled(viewport) => {
|
||||
state.tab_offset = viewport.absolute_offset().x;
|
||||
state.tab_viewport_width = viewport.bounds().width;
|
||||
@@ -245,6 +503,31 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
Task::none()
|
||||
}
|
||||
|
||||
/// Commits the captured shortcut if it is valid and unused.
|
||||
fn apply_shortcut(state: &mut Moorly) {
|
||||
let Some(capture) = state.shortcut_capture.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let action = capture.action;
|
||||
|
||||
let Some(candidate) = capture.candidate.clone() else {
|
||||
if let Some(capture) = state.shortcut_capture.as_mut() {
|
||||
capture.error = Some("Press a key combination first.".to_owned());
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
if let Some(name) = state.shortcuts.conflict(&candidate, action) {
|
||||
if let Some(capture) = state.shortcut_capture.as_mut() {
|
||||
capture.error = Some(format!("Already used by {name}."));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
state.shortcuts.set(action, Some(candidate));
|
||||
state.shortcut_capture = None;
|
||||
}
|
||||
|
||||
fn scroll_tabs_by(x: f32) -> Task<Message> {
|
||||
operation::scroll_by(TAB_SCROLL_ID, operation::AbsoluteOffset { x, y: 0.0 })
|
||||
}
|
||||
@@ -373,6 +656,117 @@ fn tab_view(tab: Tab, active: bool) -> Element<'static, Message> {
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Theme selection: a label followed by one button per preference.
|
||||
fn theme_picker(preference: theme::Preference) -> Element<'static, Message> {
|
||||
let option = |pref: theme::Preference| {
|
||||
let item = button(text(pref.label()))
|
||||
.on_press(Message::SetTheme(pref))
|
||||
.padding([6, 12]);
|
||||
|
||||
if pref == preference {
|
||||
item.style(theme::selected_option)
|
||||
} else {
|
||||
item.style(theme::outlined_option)
|
||||
}
|
||||
};
|
||||
|
||||
row![
|
||||
text("Themes").size(16),
|
||||
option(theme::Preference::System),
|
||||
option(theme::Preference::Light),
|
||||
option(theme::Preference::Dark),
|
||||
]
|
||||
.spacing(8)
|
||||
.align_y(Alignment::Center)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Shortcut bindings list.
|
||||
fn shortcuts_panel(state: &Moorly) -> Element<'_, Message> {
|
||||
let mut panel = column![
|
||||
shortcut_row(
|
||||
ShortcutAction::Settings,
|
||||
state.shortcuts.get(ShortcutAction::Settings),
|
||||
),
|
||||
shortcut_row(ShortcutAction::Quit, state.shortcuts.get(ShortcutAction::Quit)),
|
||||
]
|
||||
.spacing(8);
|
||||
|
||||
match &state.shortcut_capture {
|
||||
Some(capture) => panel = panel.push(shortcut_capture_panel(capture)),
|
||||
None => {
|
||||
panel = panel.push(text("Press ... to change a shortcut.").size(12));
|
||||
}
|
||||
}
|
||||
|
||||
panel.into()
|
||||
}
|
||||
|
||||
/// One read-only binding row with buttons to clear or rebind it.
|
||||
fn shortcut_row(
|
||||
action: ShortcutAction,
|
||||
shortcut: Option<&Shortcut>,
|
||||
) -> Element<'static, Message> {
|
||||
let value = shortcut.map(ToString::to_string).unwrap_or_default();
|
||||
|
||||
// No `on_input`: the field only displays the binding.
|
||||
let field = text_input("None", &value).width(180).padding(6);
|
||||
|
||||
row![
|
||||
container(text(action.label())).width(120),
|
||||
field,
|
||||
button("None")
|
||||
.on_press(Message::ClearShortcut(action))
|
||||
.style(theme::outlined_option),
|
||||
button("...")
|
||||
.on_press(Message::OpenShortcutCapture(action))
|
||||
.style(theme::outlined_option),
|
||||
]
|
||||
.spacing(8)
|
||||
.align_y(Alignment::Center)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Popover shown while a new shortcut is being recorded.
|
||||
fn shortcut_capture_panel(capture: &ShortcutCapture) -> Element<'_, Message> {
|
||||
let preview = capture
|
||||
.candidate
|
||||
.as_ref()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_else(|| "Press a key combination".to_owned());
|
||||
|
||||
let mut panel = column![
|
||||
text(format!("Enter a new shortcut for {}", capture.action.label())).size(12),
|
||||
container(text(preview).size(16))
|
||||
.width(Length::Fill)
|
||||
.padding([6, 10])
|
||||
.style(theme::settings_frame),
|
||||
]
|
||||
.spacing(6);
|
||||
|
||||
if let Some(error) = &capture.error {
|
||||
panel = panel.push(text(error).size(12).color(theme::DANGER));
|
||||
}
|
||||
|
||||
panel = panel.push(
|
||||
row![
|
||||
button("Apply")
|
||||
.on_press(Message::ApplyShortcut)
|
||||
.style(theme::outlined_option),
|
||||
button("Cancel")
|
||||
.on_press(Message::CancelShortcut)
|
||||
.style(theme::outlined_option),
|
||||
]
|
||||
.spacing(6),
|
||||
);
|
||||
|
||||
container(panel)
|
||||
.width(280)
|
||||
.padding(10)
|
||||
.style(theme::menu)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||
let menu_item = |section: SettingsSection| {
|
||||
let label = container(text(section.label()))
|
||||
@@ -384,7 +778,7 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||
.padding([6, 10]);
|
||||
|
||||
if section == state.settings_section {
|
||||
item.style(button::background)
|
||||
item.style(theme::selected_option)
|
||||
} else {
|
||||
item.style(button::text)
|
||||
}
|
||||
@@ -395,7 +789,18 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||
.height(Length::Fill)
|
||||
.padding(8);
|
||||
|
||||
let content = container(text(state.settings_section.label()).size(18))
|
||||
let mut content = column![].spacing(12);
|
||||
if state.settings_section != SettingsSection::Appearance {
|
||||
content = content.push(text(state.settings_section.label()).size(18));
|
||||
}
|
||||
if state.settings_section == SettingsSection::Appearance {
|
||||
content = content.push(theme_picker(state.theme));
|
||||
}
|
||||
if state.settings_section == SettingsSection::Shortcuts {
|
||||
content = content.push(shortcuts_panel(state));
|
||||
}
|
||||
|
||||
let content = container(content)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.padding(16);
|
||||
@@ -417,7 +822,7 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||
/// vertical bar and shortcut live inside the same clickable button.
|
||||
fn menu_item(
|
||||
label: &'static str,
|
||||
shortcut: Option<&'static str>,
|
||||
shortcut: Option<String>,
|
||||
message: Message,
|
||||
) -> iced::widget::Button<'static, Message> {
|
||||
let mut content = row![text(label)]
|
||||
@@ -442,7 +847,7 @@ fn menu_item(
|
||||
/// Vertical offset of the Tabs row inside the menu column, so the chevron
|
||||
/// column can place its `< |` alongside it. Keep in sync with the layout.
|
||||
fn tabs_row_top() -> f32 {
|
||||
let mut top = THEME_FIELD_HEIGHT + 4.0 + MENU_ITEM_HEIGHT + 4.0;
|
||||
let mut top = MENU_ITEM_HEIGHT + 4.0;
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
top += MENU_ITEM_HEIGHT + 4.0;
|
||||
@@ -532,43 +937,27 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
||||
return stack![base].into();
|
||||
}
|
||||
|
||||
let theme_field = container(
|
||||
stack![
|
||||
container(
|
||||
pick_list(
|
||||
theme::Preference::ALL,
|
||||
Some(state.theme),
|
||||
Message::SetTheme
|
||||
)
|
||||
.width(Length::Fill),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.padding(Padding::ZERO.top(6.0)),
|
||||
container(
|
||||
container(text("Theme").size(11))
|
||||
.padding([0, 4])
|
||||
.style(theme::field_label),
|
||||
)
|
||||
.padding(Padding::ZERO.left(8.0)),
|
||||
]
|
||||
.width(Length::Fill),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.height(THEME_FIELD_HEIGHT)
|
||||
.align_y(Alignment::Center);
|
||||
let menu_items = column![].spacing(4);
|
||||
|
||||
let menu_items = column![theme_field].spacing(4);
|
||||
let settings_shortcut = state
|
||||
.shortcuts
|
||||
.get(ShortcutAction::Settings)
|
||||
.map(ToString::to_string);
|
||||
let quit_shortcut = state
|
||||
.shortcuts
|
||||
.get(ShortcutAction::Quit)
|
||||
.map(ToString::to_string);
|
||||
|
||||
let mut menu_items = menu_items.push(menu_item(
|
||||
"Settings",
|
||||
Some("Ctrl+,"),
|
||||
settings_shortcut,
|
||||
Message::SelectTab(Tab::Settings),
|
||||
));
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
menu_items = menu_items.push(menu_item(
|
||||
"temp Add Tab",
|
||||
Some("Ctrl+Shift+T"),
|
||||
Some("Ctrl+Shift+T".to_owned()),
|
||||
Message::AddTab,
|
||||
));
|
||||
}
|
||||
@@ -576,7 +965,7 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
||||
let menu_items = menu_items
|
||||
.push(menu_item("Tabs", None, Message::ToggleTabsSubmenu))
|
||||
.push(rule::horizontal(1))
|
||||
.push(menu_item("Quit", None, Message::Quit))
|
||||
.push(menu_item("Quit", quit_shortcut, Message::Quit))
|
||||
.width(Length::Fill);
|
||||
|
||||
// The `< |` marker owns its own column; menu rows start to its right.
|
||||
@@ -615,7 +1004,7 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
||||
column![
|
||||
menu_item(
|
||||
"Close Active Tab",
|
||||
Some("Ctrl+Shift+W"),
|
||||
Some("Ctrl+Shift+W".to_owned()),
|
||||
Message::CloseActiveTab,
|
||||
),
|
||||
menu_item("Close all other tabs", None, Message::CloseOtherTabs),
|
||||
@@ -660,12 +1049,15 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Message, Moorly, Tab, theme, update};
|
||||
use iced::keyboard;
|
||||
|
||||
fn state() -> Moorly {
|
||||
Moorly {
|
||||
menu_open: false,
|
||||
tabs_submenu_open: false,
|
||||
theme: theme::Preference::default(),
|
||||
shortcuts: super::Shortcuts::default(),
|
||||
shortcut_capture: None,
|
||||
tabs: Vec::new(),
|
||||
active_tab: None,
|
||||
settings_section: super::SettingsSection::default(),
|
||||
@@ -784,4 +1176,132 @@ mod tests {
|
||||
assert_eq!(state.active_tab, None);
|
||||
assert_eq!(state.settings_section, super::SettingsSection::General);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shortcuts_round_trip_through_text() {
|
||||
let shortcut = super::Shortcut::parse("ctrl+shift+k").unwrap();
|
||||
assert_eq!(shortcut.to_string(), "Ctrl+Shift+K");
|
||||
assert_eq!(super::Shortcut::parse("Ctrl+,").unwrap().to_string(), "Ctrl+,");
|
||||
assert_eq!(super::Shortcut::parse("Ctrl+"), None);
|
||||
assert_eq!(super::Shortcut::parse("Hyper+K"), None);
|
||||
}
|
||||
|
||||
fn press(key: keyboard::Key, modifiers: keyboard::Modifiers) -> Message {
|
||||
Message::CaptureKey { key, modifiers }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changing_the_settings_shortcut_rebinds_it() {
|
||||
let mut state = state();
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::OpenShortcutCapture(super::ShortcutAction::Settings),
|
||||
);
|
||||
let _ = update(
|
||||
&mut state,
|
||||
press(
|
||||
keyboard::Key::Character("k".into()),
|
||||
keyboard::Modifiers::CTRL | keyboard::Modifiers::SHIFT,
|
||||
),
|
||||
);
|
||||
assert_eq!(
|
||||
state.shortcut_capture.as_ref().unwrap().candidate.as_ref().unwrap().to_string(),
|
||||
"Ctrl+Shift+K"
|
||||
);
|
||||
|
||||
let _ = update(&mut state, Message::ApplyShortcut);
|
||||
|
||||
assert_eq!(
|
||||
state.shortcuts.settings.as_ref().map(ToString::to_string),
|
||||
Some("Ctrl+Shift+K".to_owned())
|
||||
);
|
||||
assert!(state.shortcut_capture.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conflicting_shortcuts_are_rejected() {
|
||||
let mut state = state();
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::OpenShortcutCapture(super::ShortcutAction::Settings),
|
||||
);
|
||||
let _ = update(
|
||||
&mut state,
|
||||
press(
|
||||
keyboard::Key::Character("w".into()),
|
||||
keyboard::Modifiers::CTRL | keyboard::Modifiers::SHIFT,
|
||||
),
|
||||
);
|
||||
let _ = update(&mut state, Message::ApplyShortcut);
|
||||
|
||||
assert_eq!(
|
||||
state.shortcuts.settings.as_ref().map(ToString::to_string),
|
||||
Some("Ctrl+,".to_owned())
|
||||
);
|
||||
assert!(state.shortcut_capture.as_ref().unwrap().error.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shortcuts_cannot_steal_each_others_keys() {
|
||||
let mut state = state();
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::OpenShortcutCapture(super::ShortcutAction::Settings),
|
||||
);
|
||||
let _ = update(
|
||||
&mut state,
|
||||
press(
|
||||
keyboard::Key::Character("q".into()),
|
||||
keyboard::Modifiers::CTRL | keyboard::Modifiers::SHIFT,
|
||||
),
|
||||
);
|
||||
let _ = update(&mut state, Message::ApplyShortcut);
|
||||
|
||||
assert_eq!(
|
||||
state.shortcuts.settings.as_ref().map(ToString::to_string),
|
||||
Some("Ctrl+,".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
state.shortcuts.quit.as_ref().map(ToString::to_string),
|
||||
Some("Ctrl+Shift+Q".to_owned())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_keys_are_not_accepted_as_shortcuts() {
|
||||
let mut state = state();
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::OpenShortcutCapture(super::ShortcutAction::Settings),
|
||||
);
|
||||
let _ = update(
|
||||
&mut state,
|
||||
press(keyboard::Key::Character("k".into()), keyboard::Modifiers::NONE),
|
||||
);
|
||||
|
||||
assert!(state.shortcut_capture.as_ref().unwrap().candidate.is_none());
|
||||
assert!(state.shortcut_capture.as_ref().unwrap().error.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clearing_a_shortcut_removes_only_that_binding() {
|
||||
let mut state = state();
|
||||
let _ = update(
|
||||
&mut state,
|
||||
Message::ClearShortcut(super::ShortcutAction::Settings),
|
||||
);
|
||||
|
||||
assert!(state.shortcuts.settings.is_none());
|
||||
assert!(state.shortcuts.quit.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quit_has_a_default_shortcut() {
|
||||
let state = state();
|
||||
|
||||
assert_eq!(
|
||||
state.shortcuts.quit.as_ref().map(ToString::to_string),
|
||||
Some("Ctrl+Shift+Q".to_owned())
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user