From 8566c6f31f570552004ba07be2f0090268e0cf8b Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 27 Oct 2023 09:30:52 -0600 Subject: [PATCH] Add configuration and new tab button --- src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++------- src/menu.rs | 4 ++-- src/tab.rs | 13 +++++++++++-- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9d12ee2..b675ed0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,12 @@ use cosmic::{ app::{message, Command, Core, Settings}, executor, - iced::{widget::text, Length, Limits}, - widget::{self, icon, segmented_button, view_switcher}, + iced::{ + widget::{row, text}, + Alignment, Length, Limits, + }, + style, + widget::{self, button, icon, segmented_button, view_switcher}, ApplicationExt, Element, }; use cosmic_text::{FontSystem, SyntaxSystem, ViMode}; @@ -42,10 +46,23 @@ fn main() -> Result<(), Box> { Ok(()) } +#[derive(Clone, Debug)] +pub struct Config { + wrap: bool, +} + +impl Config { + //TODO: load from cosmic-config + pub fn new() -> Self { + Self { wrap: false } + } +} + pub struct App { core: Core, projects: Vec, tab_model: segmented_button::SingleSelectModel, + config: Config, } #[allow(dead_code)] @@ -58,6 +75,7 @@ pub enum Message { TabActivate(segmented_button::Entity), TabClose(segmented_button::Entity), Todo, + Wrap(bool), } impl App { @@ -80,6 +98,7 @@ impl App { pub fn open_tab(&mut self, path_opt: Option) { let mut tab = Tab::new(); + tab.set_config(&self.config); if let Some(path) = path_opt { tab.open(path); } @@ -131,6 +150,7 @@ impl cosmic::Application for App { core, projects: Vec::new(), tab_model: segmented_button::Model::builder().build(), + config: Config::new(), }; for arg in env::args().skip(1) { @@ -222,23 +242,40 @@ impl cosmic::Application for App { Message::Todo => { log::warn!("TODO"); } + Message::Wrap(wrap) => { + self.config.wrap = wrap; + //TODO: provide iterator over data + let entities: Vec<_> = self.tab_model.iter().collect(); + for entity in entities { + if let Some(tab) = self.tab_model.data_mut::(entity) { + tab.set_config(&self.config); + } + } + } } Command::none() } fn header_start(&self) -> Vec> { - vec![menu_bar()] + vec![menu_bar(&self.config)] } fn view(&self) -> Element { let mut tab_column = widget::column::with_capacity(3).padding([0, 16]); tab_column = tab_column.push( - view_switcher::horizontal(&self.tab_model) - .on_activate(Message::TabActivate) - .on_close(Message::TabClose) - .width(Length::Shrink), + row![ + view_switcher::horizontal(&self.tab_model) + .on_activate(Message::TabActivate) + .on_close(Message::TabClose) + .width(Length::Shrink), + button(icon::from_name("list-add-symbolic").size(16).icon()) + .on_press(Message::New) + .padding(8) + .style(style::Button::Icon) + ] + .align_items(Alignment::Center), ); match self.active_tab() { diff --git a/src/menu.rs b/src/menu.rs index 79c2154..bd9af8c 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -11,9 +11,9 @@ use cosmic::{ Element, }; -use crate::Message; +use crate::{Config, Message}; -pub fn menu_bar<'a>() -> Element<'a, Message> { +pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> { //TODO: port to libcosmic let menu_root = |label| { widget::button(label) diff --git a/src/tab.rs b/src/tab.rs index 30af24e..42d7fb6 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -1,9 +1,9 @@ // SPDX-License-Identifier: GPL-3.0-only -use cosmic_text::{Attrs, Buffer, Edit, Metrics, SyntaxEditor, ViEditor}; +use cosmic_text::{Attrs, Buffer, Edit, Metrics, SyntaxEditor, ViEditor, Wrap}; use std::{fs, path::PathBuf, sync::Mutex}; -use crate::{FONT_SYSTEM, SYNTAX_SYSTEM}; +use crate::{Config, FONT_SYSTEM, SYNTAX_SYSTEM}; static FONT_SIZES: &'static [Metrics] = &[ Metrics::new(10.0, 14.0), // Caption @@ -41,6 +41,15 @@ impl Tab { } } + pub fn set_config(&mut self, config: &Config) { + let mut editor = self.editor.lock().unwrap(); + let mut font_system = FONT_SYSTEM.lock().unwrap(); + let mut editor = editor.borrow_with(&mut font_system); + editor + .buffer_mut() + .set_wrap(if config.wrap { Wrap::Word } else { Wrap::None }); + } + pub fn open(&mut self, path: PathBuf) { let mut editor = self.editor.lock().unwrap(); let mut font_system = FONT_SYSTEM.lock().unwrap();