Added tabbed interface, Settings tab
This commit is contained in:
1 parent
328557ed2d
commit
143f43a407
2 files changed
+304
-14
No files matched your search
+274
-14
@@ -1,22 +1,82 @@
|
|||||||
use iced::widget::{button, column, container, pick_list, row, stack, text};
|
use iced::widget::{
|
||||||
use iced::{Alignment, Element, Length, Padding, Task};
|
button, column, container, mouse_area, pick_list, row, rule, space, stack, text, tooltip,
|
||||||
|
};
|
||||||
|
use iced::{Alignment, Element, Length, Padding, Subscription, Task, keyboard};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
mod terminal;
|
mod terminal;
|
||||||
mod theme;
|
mod theme;
|
||||||
|
|
||||||
const MENUBAR_HEIGHT: f32 = 21.0;
|
const MENUBAR_HEIGHT: f32 = 21.0;
|
||||||
|
/// Space kept clear for the hamburger/close button at the end of the menubar.
|
||||||
|
const HAMBURGER_WIDTH: f32 = 32.0;
|
||||||
|
/// Longest tab title shown before it gets ellipsized.
|
||||||
|
const MAX_TAB_TITLE: usize = 20;
|
||||||
|
/// Fixed height for rows in the hamburger menu.
|
||||||
|
const MENU_ITEM_HEIGHT: f32 = 24.0;
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
iced::application(Moorly::default, update, view)
|
iced::application(Moorly::default, update, view)
|
||||||
.title("Moorly")
|
.title("Moorly")
|
||||||
.theme(|state: &Moorly| state.theme.iced())
|
.theme(|state: &Moorly| state.theme.iced())
|
||||||
|
.subscription(subscription)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscription(_state: &Moorly) -> Subscription<Message> {
|
||||||
|
keyboard::listen().filter_map(|event| match event {
|
||||||
|
keyboard::Event::KeyPressed {
|
||||||
|
key: keyboard::Key::Character(key),
|
||||||
|
modifiers,
|
||||||
|
..
|
||||||
|
} if key.as_str() == "," && modifiers.control() => {
|
||||||
|
Some(Message::SelectTab(Tab::Settings))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
struct Moorly {
|
struct Moorly {
|
||||||
menu_open: bool,
|
menu_open: bool,
|
||||||
theme: theme::Preference,
|
theme: theme::Preference,
|
||||||
|
tabs: Vec<Tab>,
|
||||||
|
active_tab: Option<Tab>,
|
||||||
|
settings_section: SettingsSection,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Tab {
|
||||||
|
Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tab {
|
||||||
|
fn label(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Settings => "Settings",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
|
enum SettingsSection {
|
||||||
|
#[default]
|
||||||
|
General,
|
||||||
|
Startup,
|
||||||
|
Appearance,
|
||||||
|
Profiles,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SettingsSection {
|
||||||
|
const ALL: [Self; 4] = [Self::General, Self::Startup, Self::Appearance, Self::Profiles];
|
||||||
|
|
||||||
|
fn label(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::General => "General",
|
||||||
|
Self::Startup => "Startup",
|
||||||
|
Self::Appearance => "Appearance",
|
||||||
|
Self::Profiles => "Profiles",
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Moorly {
|
impl Default for Moorly {
|
||||||
@@ -24,6 +84,9 @@ impl Default for Moorly {
|
|||||||
Self {
|
Self {
|
||||||
menu_open: false,
|
menu_open: false,
|
||||||
theme: theme::load(),
|
theme: theme::load(),
|
||||||
|
tabs: Vec::new(),
|
||||||
|
active_tab: None,
|
||||||
|
settings_section: SettingsSection::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,18 +94,41 @@ impl Default for Moorly {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
ToggleMenu,
|
ToggleMenu,
|
||||||
|
CloseMenu,
|
||||||
SetTheme(theme::Preference),
|
SetTheme(theme::Preference),
|
||||||
|
SelectTab(Tab),
|
||||||
|
CloseTab(Tab),
|
||||||
|
SelectSettingsSection(SettingsSection),
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::ToggleMenu => state.menu_open = !state.menu_open,
|
Message::ToggleMenu => state.menu_open = !state.menu_open,
|
||||||
|
Message::CloseMenu => state.menu_open = false,
|
||||||
Message::SetTheme(preference) => {
|
Message::SetTheme(preference) => {
|
||||||
state.theme = preference;
|
state.theme = preference;
|
||||||
state.menu_open = false;
|
state.menu_open = false;
|
||||||
theme::save(preference);
|
theme::save(preference);
|
||||||
}
|
}
|
||||||
|
Message::SelectTab(tab) => {
|
||||||
|
if !state.tabs.contains(&tab) {
|
||||||
|
state.tabs.push(tab);
|
||||||
|
}
|
||||||
|
state.active_tab = Some(tab);
|
||||||
|
state.menu_open = false;
|
||||||
|
}
|
||||||
|
Message::CloseTab(tab) => {
|
||||||
|
state.tabs.retain(|open| *open != tab);
|
||||||
|
if state.active_tab == Some(tab) {
|
||||||
|
state.active_tab = state.tabs.first().copied();
|
||||||
|
}
|
||||||
|
// Drop the tab's page state along with the tab itself.
|
||||||
|
if tab == Tab::Settings {
|
||||||
|
state.settings_section = SettingsSection::default();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::SelectSettingsSection(section) => state.settings_section = section,
|
||||||
Message::Quit => std::process::exit(0),
|
Message::Quit => std::process::exit(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,21 +156,146 @@ fn hamburger(open: bool) -> iced::widget::Button<'static, Message> {
|
|||||||
.style(button::text)
|
.style(button::text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shortens a title to `MAX_TAB_TITLE` characters, ellipsizing the rest.
|
||||||
|
fn fit_title(title: &str) -> String {
|
||||||
|
if title.chars().count() <= MAX_TAB_TITLE {
|
||||||
|
return title.to_owned();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut fitted: String = title.chars().take(MAX_TAB_TITLE - 3).collect();
|
||||||
|
fitted.push_str("...");
|
||||||
|
fitted
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tab_view(tab: Tab, active: bool) -> Element<'static, Message> {
|
||||||
|
let title = tab.label();
|
||||||
|
|
||||||
|
let content = container(
|
||||||
|
row![
|
||||||
|
button(text(fit_title(title)).size(12))
|
||||||
|
.on_press(Message::SelectTab(tab))
|
||||||
|
.padding([1, 4])
|
||||||
|
.style(button::text),
|
||||||
|
button(text("✕").size(10))
|
||||||
|
.on_press(Message::CloseTab(tab))
|
||||||
|
.padding([1, 4])
|
||||||
|
.style(button::text),
|
||||||
|
]
|
||||||
|
.spacing(0)
|
||||||
|
.align_y(Alignment::Center),
|
||||||
|
)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.align_y(Alignment::Center)
|
||||||
|
.style(move |theme| theme::tab(theme, active));
|
||||||
|
|
||||||
|
tooltip(
|
||||||
|
content,
|
||||||
|
container(text(title).size(11)).padding(4),
|
||||||
|
tooltip::Position::Bottom,
|
||||||
|
)
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||||
|
let menu_item = |section: SettingsSection| {
|
||||||
|
let label = container(text(section.label()))
|
||||||
|
.width(Length::Fill)
|
||||||
|
.align_x(Alignment::Start);
|
||||||
|
let item = button(label)
|
||||||
|
.on_press(Message::SelectSettingsSection(section))
|
||||||
|
.width(Length::Fill)
|
||||||
|
.padding([6, 10]);
|
||||||
|
|
||||||
|
if section == state.settings_section {
|
||||||
|
item.style(button::background)
|
||||||
|
} else {
|
||||||
|
item.style(button::text)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let menu = container(column(SettingsSection::ALL.map(|section| menu_item(section).into())).spacing(2))
|
||||||
|
.width(160)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.padding(8);
|
||||||
|
|
||||||
|
let content = container(text(state.settings_section.label()).size(18))
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.padding(16);
|
||||||
|
|
||||||
|
let frame = container(row![menu, rule::vertical(1), content])
|
||||||
|
.width(800)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.style(theme::settings_frame);
|
||||||
|
|
||||||
|
container(frame)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.center(Length::Fill)
|
||||||
|
.padding(24)
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A left-aligned menu row with an optional right-aligned shortcut. The
|
||||||
|
/// vertical bar and shortcut live inside the same clickable button.
|
||||||
|
fn menu_item(
|
||||||
|
label: &'static str,
|
||||||
|
shortcut: Option<&'static str>,
|
||||||
|
message: Message,
|
||||||
|
) -> iced::widget::Button<'static, Message> {
|
||||||
|
let mut content = row![text(label)]
|
||||||
|
.width(Length::Fill)
|
||||||
|
.align_y(Alignment::Center);
|
||||||
|
|
||||||
|
if let Some(shortcut) = shortcut {
|
||||||
|
content = content
|
||||||
|
.push(space::horizontal())
|
||||||
|
.push(container(rule::vertical(12)).padding([0, 6]))
|
||||||
|
.push(text(shortcut).size(11));
|
||||||
|
}
|
||||||
|
|
||||||
|
button(content)
|
||||||
|
.on_press(message)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(MENU_ITEM_HEIGHT)
|
||||||
|
.padding([4, 8])
|
||||||
|
.style(button::text)
|
||||||
|
}
|
||||||
|
|
||||||
fn view(state: &Moorly) -> Element<'_, Message> {
|
fn view(state: &Moorly) -> Element<'_, Message> {
|
||||||
let mut bar = row![].align_y(Alignment::Center);
|
// Tab strip: push tab widgets into this row as they get added.
|
||||||
if !state.menu_open {
|
let mut tab_row = row![].spacing(2);
|
||||||
|
for &tab in &state.tabs {
|
||||||
|
tab_row = tab_row.push(tab_view(tab, state.active_tab == Some(tab)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let tabs = container(tab_row)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.align_y(Alignment::Center);
|
||||||
|
|
||||||
|
let mut bar = row![tabs].align_y(Alignment::Center);
|
||||||
|
if state.menu_open {
|
||||||
|
// The dropdown draws the close button over this spot.
|
||||||
|
bar = bar.push(space::horizontal().width(HAMBURGER_WIDTH));
|
||||||
|
} else {
|
||||||
bar = bar.push(hamburger(false));
|
bar = bar.push(hamburger(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
let menubar = container(bar)
|
let menubar = container(bar)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(MENUBAR_HEIGHT)
|
.height(MENUBAR_HEIGHT)
|
||||||
.align_x(Alignment::End)
|
|
||||||
.align_y(Alignment::Center)
|
.align_y(Alignment::Center)
|
||||||
.padding([0, 8])
|
.padding([0, 8])
|
||||||
.style(theme::menubar);
|
.style(theme::menubar);
|
||||||
|
|
||||||
let main_area = container(column![]).width(Length::Fill).height(Length::Fill);
|
let main_area: Element<'_, Message> = match state.active_tab {
|
||||||
|
Some(Tab::Settings) => settings_page(state),
|
||||||
|
None => container(column![])
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.into(),
|
||||||
|
};
|
||||||
|
|
||||||
let base = column![menubar, main_area];
|
let base = column![menubar, main_area];
|
||||||
|
|
||||||
@@ -114,12 +325,14 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
|||||||
|
|
||||||
let menu_items = column![theme_field].spacing(4);
|
let menu_items = column![theme_field].spacing(4);
|
||||||
|
|
||||||
let menu_items = menu_items.push(
|
let menu_items = menu_items
|
||||||
button("Quit")
|
.push(menu_item(
|
||||||
.on_press(Message::Quit)
|
"Settings",
|
||||||
.width(Length::Fill)
|
Some("Ctrl+,"),
|
||||||
.style(button::text),
|
Message::SelectTab(Tab::Settings),
|
||||||
);
|
))
|
||||||
|
.push(rule::horizontal(1))
|
||||||
|
.push(menu_item("Quit", None, Message::Quit));
|
||||||
|
|
||||||
let menu = container(
|
let menu = container(
|
||||||
column![
|
column![
|
||||||
@@ -130,7 +343,7 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
|||||||
]
|
]
|
||||||
.spacing(2),
|
.spacing(2),
|
||||||
)
|
)
|
||||||
.width(160)
|
.width(200)
|
||||||
.padding(4)
|
.padding(4)
|
||||||
.style(theme::menu);
|
.style(theme::menu);
|
||||||
|
|
||||||
@@ -141,8 +354,55 @@ fn view(state: &Moorly) -> Element<'_, Message> {
|
|||||||
.align_y(Alignment::Start)
|
.align_y(Alignment::Start)
|
||||||
.padding(Padding::ZERO.right(8.0));
|
.padding(Padding::ZERO.right(8.0));
|
||||||
|
|
||||||
stack![base, dropdown]
|
// Clicking anywhere outside the dropdown dismisses it.
|
||||||
|
let backdrop = mouse_area(
|
||||||
|
container(space::horizontal())
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill),
|
||||||
|
)
|
||||||
|
.on_press(Message::CloseMenu);
|
||||||
|
|
||||||
|
stack![base, backdrop, dropdown]
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{Message, Moorly, Tab, theme, update};
|
||||||
|
|
||||||
|
fn state() -> Moorly {
|
||||||
|
Moorly {
|
||||||
|
menu_open: false,
|
||||||
|
theme: theme::Preference::default(),
|
||||||
|
tabs: Vec::new(),
|
||||||
|
active_tab: None,
|
||||||
|
settings_section: super::SettingsSection::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn long_titles_are_ellipsized_to_max_width() {
|
||||||
|
assert_eq!(super::fit_title("Settings"), "Settings");
|
||||||
|
assert_eq!(super::fit_title(&"a".repeat(20)), "a".repeat(20));
|
||||||
|
|
||||||
|
let fitted = super::fit_title(&"a".repeat(30));
|
||||||
|
assert_eq!(fitted, format!("{}...", "a".repeat(17)));
|
||||||
|
assert_eq!(fitted.chars().count(), 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn closing_active_tab_unloads_it() {
|
||||||
|
let mut state = state();
|
||||||
|
let _ = update(&mut state, Message::SelectTab(Tab::Settings));
|
||||||
|
assert_eq!(state.tabs, vec![Tab::Settings]);
|
||||||
|
assert_eq!(state.active_tab, Some(Tab::Settings));
|
||||||
|
|
||||||
|
let _ = update(&mut state, Message::SelectSettingsSection(super::SettingsSection::Startup));
|
||||||
|
let _ = update(&mut state, Message::CloseTab(Tab::Settings));
|
||||||
|
assert!(state.tabs.is_empty());
|
||||||
|
assert_eq!(state.active_tab, None);
|
||||||
|
assert_eq!(state.settings_section, super::SettingsSection::General);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -116,6 +116,36 @@ pub fn menu(theme: &Theme) -> container::Style {
|
|||||||
style
|
style
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Style for a tab in the menubar; active tabs sit on the base surface.
|
||||||
|
pub fn tab(theme: &Theme, active: bool) -> container::Style {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
container::Style {
|
||||||
|
background: active.then(|| palette.background.base.color.into()),
|
||||||
|
border: Border {
|
||||||
|
color: palette.background.strong.color,
|
||||||
|
width: if active { 1.0 } else { 0.0 },
|
||||||
|
radius: 3.0.into(),
|
||||||
|
},
|
||||||
|
..container::Style::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Style for the settings window frame.
|
||||||
|
pub fn settings_frame(theme: &Theme) -> container::Style {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
container::Style {
|
||||||
|
background: Some(palette.background.base.color.into()),
|
||||||
|
border: Border {
|
||||||
|
color: palette.background.strong.color,
|
||||||
|
width: 1.0,
|
||||||
|
radius: 6.0.into(),
|
||||||
|
},
|
||||||
|
..container::Style::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Style for a label that sits over the outline of an input.
|
/// Style for a label that sits over the outline of an input.
|
||||||
pub fn field_label(theme: &Theme) -> container::Style {
|
pub fn field_label(theme: &Theme) -> container::Style {
|
||||||
container::Style {
|
container::Style {
|
||||||
|
|||||||
Reference in new issue
Block a user