Add configuration and new tab button
This commit is contained in:
parent
4b25e866b5
commit
8566c6f31f
3 changed files with 57 additions and 11 deletions
51
src/main.rs
51
src/main.rs
|
|
@ -3,8 +3,12 @@
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
app::{message, Command, Core, Settings},
|
app::{message, Command, Core, Settings},
|
||||||
executor,
|
executor,
|
||||||
iced::{widget::text, Length, Limits},
|
iced::{
|
||||||
widget::{self, icon, segmented_button, view_switcher},
|
widget::{row, text},
|
||||||
|
Alignment, Length, Limits,
|
||||||
|
},
|
||||||
|
style,
|
||||||
|
widget::{self, button, icon, segmented_button, view_switcher},
|
||||||
ApplicationExt, Element,
|
ApplicationExt, Element,
|
||||||
};
|
};
|
||||||
use cosmic_text::{FontSystem, SyntaxSystem, ViMode};
|
use cosmic_text::{FontSystem, SyntaxSystem, ViMode};
|
||||||
|
|
@ -42,10 +46,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
Ok(())
|
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 {
|
pub struct App {
|
||||||
core: Core,
|
core: Core,
|
||||||
projects: Vec<Project>,
|
projects: Vec<Project>,
|
||||||
tab_model: segmented_button::SingleSelectModel,
|
tab_model: segmented_button::SingleSelectModel,
|
||||||
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
@ -58,6 +75,7 @@ pub enum Message {
|
||||||
TabActivate(segmented_button::Entity),
|
TabActivate(segmented_button::Entity),
|
||||||
TabClose(segmented_button::Entity),
|
TabClose(segmented_button::Entity),
|
||||||
Todo,
|
Todo,
|
||||||
|
Wrap(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
|
|
@ -80,6 +98,7 @@ impl App {
|
||||||
|
|
||||||
pub fn open_tab(&mut self, path_opt: Option<PathBuf>) {
|
pub fn open_tab(&mut self, path_opt: Option<PathBuf>) {
|
||||||
let mut tab = Tab::new();
|
let mut tab = Tab::new();
|
||||||
|
tab.set_config(&self.config);
|
||||||
if let Some(path) = path_opt {
|
if let Some(path) = path_opt {
|
||||||
tab.open(path);
|
tab.open(path);
|
||||||
}
|
}
|
||||||
|
|
@ -131,6 +150,7 @@ impl cosmic::Application for App {
|
||||||
core,
|
core,
|
||||||
projects: Vec::new(),
|
projects: Vec::new(),
|
||||||
tab_model: segmented_button::Model::builder().build(),
|
tab_model: segmented_button::Model::builder().build(),
|
||||||
|
config: Config::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
for arg in env::args().skip(1) {
|
for arg in env::args().skip(1) {
|
||||||
|
|
@ -222,23 +242,40 @@ impl cosmic::Application for App {
|
||||||
Message::Todo => {
|
Message::Todo => {
|
||||||
log::warn!("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()
|
Command::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn header_start(&self) -> Vec<Element<Message>> {
|
fn header_start(&self) -> Vec<Element<Message>> {
|
||||||
vec![menu_bar()]
|
vec![menu_bar(&self.config)]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
let mut tab_column = widget::column::with_capacity(3).padding([0, 16]);
|
let mut tab_column = widget::column::with_capacity(3).padding([0, 16]);
|
||||||
|
|
||||||
tab_column = tab_column.push(
|
tab_column = tab_column.push(
|
||||||
view_switcher::horizontal(&self.tab_model)
|
row![
|
||||||
.on_activate(Message::TabActivate)
|
view_switcher::horizontal(&self.tab_model)
|
||||||
.on_close(Message::TabClose)
|
.on_activate(Message::TabActivate)
|
||||||
.width(Length::Shrink),
|
.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() {
|
match self.active_tab() {
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,9 @@ use cosmic::{
|
||||||
Element,
|
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
|
//TODO: port to libcosmic
|
||||||
let menu_root = |label| {
|
let menu_root = |label| {
|
||||||
widget::button(label)
|
widget::button(label)
|
||||||
|
|
|
||||||
13
src/tab.rs
13
src/tab.rs
|
|
@ -1,9 +1,9 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// 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 std::{fs, path::PathBuf, sync::Mutex};
|
||||||
|
|
||||||
use crate::{FONT_SYSTEM, SYNTAX_SYSTEM};
|
use crate::{Config, FONT_SYSTEM, SYNTAX_SYSTEM};
|
||||||
|
|
||||||
static FONT_SIZES: &'static [Metrics] = &[
|
static FONT_SIZES: &'static [Metrics] = &[
|
||||||
Metrics::new(10.0, 14.0), // Caption
|
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) {
|
pub fn open(&mut self, path: PathBuf) {
|
||||||
let mut editor = self.editor.lock().unwrap();
|
let mut editor = self.editor.lock().unwrap();
|
||||||
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue