libcosmic/examples/cosmic/src/window/editor.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

use cosmic::iced::widget::{horizontal_space, row};
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
use cosmic::iced::{Alignment, Length};
use cosmic::widget::{button, icon, segmented_button, view_switcher};
use cosmic::{theme, Apply, Element};
use slotmap::Key;
#[derive(Clone, Copy, Debug)]
pub enum Message {
Activate(segmented_button::Entity),
AddNew,
Close(segmented_button::Entity),
}
pub struct State {
pub pages: segmented_button::SingleSelectModel,
}
impl Default for State {
fn default() -> Self {
let mut state = Self {
pages: segmented_button::Model::default(),
};
let id = state.tab_add_new();
state.pages.activate(id);
state
}
}
impl State {
pub(super) fn update(&mut self, message: Message) {
match message {
Message::Activate(id) => self.pages.activate(id),
Message::AddNew => {
self.tab_add_new();
}
Message::Close(id) => self.tab_close(id),
}
}
pub fn tab_add_new(&mut self) -> segmented_button::Entity {
let id = self.pages.insert().closable().id();
self.pages
.text_set(id, format!("Tab {}", id.data().as_ffi() & 0xffff_ffff));
id
}
pub fn tab_close(&mut self, id: segmented_button::Entity) {
if self.pages.is_active(id) {
if let Some(pos) = self.pages.position(id) {
let next = if pos == 0 { pos + 1 } else { pos - 1 };
self.pages.activate_position(next);
}
}
self.pages.remove(id);
}
pub(super) fn view<'a>(&'a self, _window: &'a super::Window) -> Element<'a, Message> {
let tabs = view_switcher::horizontal(&self.pages)
.show_close_icon_on_hover(true)
.on_activate(Message::Activate)
.on_close(Message::Close)
.width(Length::Shrink);
2023-09-13 16:01:04 +02:00
let new_tab_button = icon::from_name("tab-new-symbolic")
.size(20)
.apply(button::icon)
.on_press(Message::AddNew);
let tab_header = row!(tabs, new_tab_button).align_items(Alignment::Center);
row!(tab_header, horizontal_space(Length::Fill)).into()
}
}