Add menu
This commit is contained in:
parent
a342ff266d
commit
db738371ac
6 changed files with 184 additions and 74 deletions
|
|
@ -56,7 +56,7 @@ impl Config {
|
|||
let font_size = f32::from(self.font_size).max(1.0);
|
||||
let adj = f32::from(zoom_adj);
|
||||
let adj_step = f32::from(self.font_size_zoom_step_mul_100) / 100.0;
|
||||
(font_size + adj*adj_step).max(1.0)
|
||||
(font_size + adj * adj_step).max(1.0)
|
||||
}
|
||||
|
||||
// Calculate metrics from font size
|
||||
|
|
|
|||
88
src/main.rs
88
src/main.rs
|
|
@ -30,12 +30,13 @@ mod config;
|
|||
|
||||
mod localize;
|
||||
|
||||
use menu::menu_bar;
|
||||
mod menu;
|
||||
|
||||
use self::terminal::{Terminal, TerminalScroll};
|
||||
use terminal::{Terminal, TerminalScroll};
|
||||
mod terminal;
|
||||
|
||||
use self::terminal_box::terminal_box;
|
||||
use terminal_box::terminal_box;
|
||||
mod terminal_box;
|
||||
|
||||
mod terminal_theme;
|
||||
|
|
@ -140,24 +141,27 @@ pub enum Message {
|
|||
Copy(Option<segmented_button::Entity>),
|
||||
DefaultFont(usize),
|
||||
DefaultFontSize(usize),
|
||||
ZoomIn,
|
||||
ZoomOut,
|
||||
ZoomReset,
|
||||
DefaultZoomStep(usize),
|
||||
Paste(Option<segmented_button::Entity>),
|
||||
PasteValue(Option<segmented_button::Entity>, String),
|
||||
SelectAll(Option<segmented_button::Entity>),
|
||||
ShowHeaderBar(bool),
|
||||
SystemThemeModeChange(cosmic_theme::ThemeMode),
|
||||
SyntaxTheme(usize, bool),
|
||||
SystemThemeModeChange(cosmic_theme::ThemeMode),
|
||||
TabActivate(segmented_button::Entity),
|
||||
TabClose(segmented_button::Entity),
|
||||
TabClose(Option<segmented_button::Entity>),
|
||||
TabContextAction(segmented_button::Entity, Action),
|
||||
TabContextMenu(segmented_button::Entity, Option<Point>),
|
||||
TabNew,
|
||||
TermEvent(segmented_button::Entity, TermEvent),
|
||||
TermEventTx(mpsc::Sender<(segmented_button::Entity, TermEvent)>),
|
||||
Todo(&'static str),
|
||||
ToggleContextPage(ContextPage),
|
||||
WindowClose,
|
||||
WindowNew,
|
||||
ZoomIn,
|
||||
ZoomOut,
|
||||
ZoomReset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
|
|
@ -473,17 +477,15 @@ impl Application for App {
|
|||
}
|
||||
}
|
||||
}
|
||||
Message::ZoomIn => {
|
||||
self.zoom_adj = self.zoom_adj.saturating_add(1);
|
||||
return self.save_config();
|
||||
},
|
||||
Message::ZoomOut => {
|
||||
self.zoom_adj = self.zoom_adj.saturating_sub(1);
|
||||
return self.save_config();
|
||||
},
|
||||
Message::ZoomReset => {
|
||||
self.zoom_adj = 0;
|
||||
return self.save_config();
|
||||
Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
|
||||
Some(font_size) => {
|
||||
self.config.font_size = *font_size;
|
||||
self.zoom_adj = 0; // reset zoom
|
||||
return self.save_config();
|
||||
}
|
||||
None => {
|
||||
log::warn!("failed to find font with index {}", index);
|
||||
}
|
||||
},
|
||||
Message::DefaultZoomStep(index) => match self.zoom_steps.get(index) {
|
||||
Some(zoom_step) => {
|
||||
|
|
@ -495,16 +497,6 @@ impl Application for App {
|
|||
log::warn!("failed to find zoom step with index {}", index);
|
||||
}
|
||||
},
|
||||
Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
|
||||
Some(font_size) => {
|
||||
self.config.font_size = *font_size;
|
||||
self.zoom_adj = 0; // reset zoom
|
||||
return self.save_config();
|
||||
}
|
||||
None => {
|
||||
log::warn!("failed to find font with index {}", index);
|
||||
}
|
||||
},
|
||||
Message::Paste(entity_opt) => {
|
||||
return clipboard::read(move |value_opt| match value_opt {
|
||||
Some(value) => message::app(Message::PasteValue(entity_opt, value)),
|
||||
|
|
@ -551,7 +543,9 @@ impl Application for App {
|
|||
self.tab_model.activate(entity);
|
||||
return self.update_title();
|
||||
}
|
||||
Message::TabClose(entity) => {
|
||||
Message::TabClose(entity_opt) => {
|
||||
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
||||
|
||||
// Activate closest item
|
||||
if let Some(position) = self.tab_model.position(entity) {
|
||||
if position > 0 {
|
||||
|
|
@ -640,7 +634,7 @@ impl Application for App {
|
|||
}
|
||||
}
|
||||
TermEvent::Exit => {
|
||||
return self.update(Message::TabClose(entity));
|
||||
return self.update(Message::TabClose(Some(entity)));
|
||||
}
|
||||
TermEvent::PtyWrite(text) => {
|
||||
if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) {
|
||||
|
|
@ -676,6 +670,9 @@ impl Application for App {
|
|||
Message::TermEventTx(term_event_tx) => {
|
||||
self.term_event_tx_opt = Some(term_event_tx);
|
||||
}
|
||||
Message::Todo(todo) => {
|
||||
log::warn!("TODO: {}", todo);
|
||||
}
|
||||
Message::ToggleContextPage(context_page) => {
|
||||
if self.context_page == context_page {
|
||||
self.core.window.show_context = !self.core.window.show_context;
|
||||
|
|
@ -685,6 +682,32 @@ impl Application for App {
|
|||
}
|
||||
self.set_context_title(context_page.title());
|
||||
}
|
||||
Message::WindowClose => {
|
||||
return window::close(window::Id::MAIN);
|
||||
}
|
||||
Message::WindowNew => match env::current_exe() {
|
||||
Ok(exe) => match process::Command::new(&exe).spawn() {
|
||||
Ok(_child) => {}
|
||||
Err(err) => {
|
||||
log::error!("failed to execute {:?}: {}", exe, err);
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
log::error!("failed to get current executable path: {}", err);
|
||||
}
|
||||
},
|
||||
Message::ZoomIn => {
|
||||
self.zoom_adj = self.zoom_adj.saturating_add(1);
|
||||
return self.save_config();
|
||||
}
|
||||
Message::ZoomOut => {
|
||||
self.zoom_adj = self.zoom_adj.saturating_sub(1);
|
||||
return self.save_config();
|
||||
}
|
||||
Message::ZoomReset => {
|
||||
self.zoom_adj = 0;
|
||||
return self.save_config();
|
||||
}
|
||||
}
|
||||
|
||||
Command::none()
|
||||
|
|
@ -707,7 +730,8 @@ impl Application for App {
|
|||
widget::button(widget::icon::from_name("list-add-symbolic").size(16).icon())
|
||||
.on_press(Message::TabNew)
|
||||
.padding(space_xxs)
|
||||
.style(style::Button::Icon)
|
||||
.style(style::Button::Icon),
|
||||
menu_bar(&self.config)
|
||||
]
|
||||
.align_items(Alignment::Center)
|
||||
.into()]
|
||||
|
|
@ -741,7 +765,7 @@ impl Application for App {
|
|||
.button_height(32)
|
||||
.button_spacing(space_xxs)
|
||||
.on_activate(Message::TabActivate)
|
||||
.on_close(Message::TabClose),
|
||||
.on_close(|entity| Message::TabClose(Some(entity))),
|
||||
)
|
||||
.style(style::Container::Background)
|
||||
.width(Length::Fill),
|
||||
|
|
|
|||
78
src/menu.rs
78
src/menu.rs
|
|
@ -3,15 +3,19 @@
|
|||
use cosmic::{
|
||||
//TODO: export in cosmic::widget
|
||||
iced::{
|
||||
widget::{column, horizontal_rule},
|
||||
widget::{column, horizontal_rule, horizontal_space},
|
||||
Alignment, Background, Length,
|
||||
},
|
||||
theme,
|
||||
widget::{self, segmented_button},
|
||||
widget::{
|
||||
self,
|
||||
menu::{ItemHeight, ItemWidth, MenuBar, MenuTree},
|
||||
segmented_button,
|
||||
},
|
||||
Element,
|
||||
};
|
||||
|
||||
use crate::{fl, Action, Config, Message};
|
||||
use crate::{fl, Action, Config, ContextPage, Message};
|
||||
|
||||
macro_rules! menu_button {
|
||||
($($x:expr),+ $(,)?) => (
|
||||
|
|
@ -75,3 +79,71 @@ pub fn context_menu<'a>(config: &Config, entity: segmented_button::Entity) -> El
|
|||
.width(Length::Fixed(240.0))
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
|
||||
//TODO: port to libcosmic
|
||||
let menu_root = |label| {
|
||||
widget::button(widget::text(label))
|
||||
.padding([4, 12])
|
||||
.style(theme::Button::MenuRoot)
|
||||
};
|
||||
|
||||
let find_key = |message: &Message| -> String {
|
||||
//TODO: hotkey config
|
||||
String::new()
|
||||
};
|
||||
|
||||
let menu_item = |label, message| {
|
||||
let key = find_key(&message);
|
||||
MenuTree::new(
|
||||
menu_button!(
|
||||
widget::text(label),
|
||||
horizontal_space(Length::Fill),
|
||||
widget::text(key)
|
||||
)
|
||||
.on_press(message),
|
||||
)
|
||||
};
|
||||
|
||||
let menu_key = |label, key, message| {
|
||||
MenuTree::new(
|
||||
menu_button!(widget::text(label), horizontal_space(Length::Fill), key)
|
||||
.on_press(message),
|
||||
)
|
||||
};
|
||||
|
||||
MenuBar::new(vec![
|
||||
MenuTree::with_children(
|
||||
menu_root(fl!("file")),
|
||||
vec![
|
||||
menu_item(fl!("new-tab"), Message::TabNew),
|
||||
menu_item(fl!("new-window"), Message::WindowNew),
|
||||
MenuTree::new(horizontal_rule(1)),
|
||||
menu_item(fl!("close-tab"), Message::TabClose(None)),
|
||||
MenuTree::new(horizontal_rule(1)),
|
||||
menu_item(fl!("quit"), Message::WindowClose),
|
||||
],
|
||||
),
|
||||
MenuTree::with_children(
|
||||
menu_root(fl!("edit")),
|
||||
vec![
|
||||
menu_item(fl!("copy"), Message::Copy(None)),
|
||||
menu_item(fl!("paste"), Message::Paste(None)),
|
||||
menu_item(fl!("select-all"), Message::SelectAll(None)),
|
||||
MenuTree::new(horizontal_rule(1)),
|
||||
menu_key(fl!("find"), "Ctrl + F", Message::Todo("find")),
|
||||
],
|
||||
),
|
||||
MenuTree::with_children(
|
||||
menu_root(fl!("view")),
|
||||
vec![menu_item(
|
||||
fl!("menu-settings"),
|
||||
Message::ToggleContextPage(ContextPage::Settings),
|
||||
)],
|
||||
),
|
||||
])
|
||||
.item_height(ItemHeight::Dynamic(40))
|
||||
.item_width(ItemWidth::Uniform(240))
|
||||
.spacing(4.0)
|
||||
.into()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -318,7 +318,12 @@ impl Terminal {
|
|||
self.update();
|
||||
}
|
||||
|
||||
pub fn set_config(&mut self, config: &crate::Config, themes: &HashMap<String, Colors>, zoom_adj: i8) {
|
||||
pub fn set_config(
|
||||
&mut self,
|
||||
config: &crate::Config,
|
||||
themes: &HashMap<String, Colors>,
|
||||
zoom_adj: i8,
|
||||
) {
|
||||
let mut update_cell_size = false;
|
||||
let mut update = false;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue