Add configuration and new tab button

This commit is contained in:
Jeremy Soller 2023-10-27 09:30:52 -06:00
parent 4b25e866b5
commit 8566c6f31f
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
3 changed files with 57 additions and 11 deletions

View file

@ -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<dyn std::error::Error>> {
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<Project>,
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<PathBuf>) {
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::<Tab>(entity) {
tab.set_config(&self.config);
}
}
}
}
Command::none()
}
fn header_start(&self) -> Vec<Element<Message>> {
vec![menu_bar()]
vec![menu_bar(&self.config)]
}
fn view(&self) -> Element<Message> {
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() {

View file

@ -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)

View file

@ -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();