From f8e7eead5f73502a652f988c1e19adabf3c42362 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Mon, 16 Dec 2024 09:51:21 -0600 Subject: [PATCH] setting fonts in update --- flake.nix | 1 + src/main.rs | 168 ++++++++++++++++++++++++++------------------ src/ui/presenter.rs | 56 +++++++-------- 3 files changed, 127 insertions(+), 98 deletions(-) diff --git a/flake.nix b/flake.nix index c15aaa9..e59b481 100644 --- a/flake.nix +++ b/flake.nix @@ -46,6 +46,7 @@ gst_all_1.gst-plugins-rs gst_all_1.gst-vaapi gst_all_1.gstreamer + gst_all_1.gstreamermm ]; bi = with pkgs; [ diff --git a/src/main.rs b/src/main.rs index 7c34ed4..6f0fb4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,21 +2,20 @@ use clap::{command, Parser}; use core::service_items::{ServiceItem, ServiceItemModel}; use cosmic::app::context_drawer::ContextDrawer; use cosmic::app::{Core, Settings, Task}; -use cosmic::iced::alignment::Horizontal; -use cosmic::iced::keyboard::Key; +use cosmic::iced::keyboard::{Key, Modifiers}; use cosmic::iced::window::{Mode, Position}; use cosmic::iced::{ self, event, window, Font, Length, Padding, Point, }; use cosmic::iced_core::SmolStr; +use cosmic::iced_futures::Subscription; use cosmic::iced_widget::{column, row, stack}; use cosmic::prelude::ElementExt; use cosmic::prelude::*; -use cosmic::widget::aspect_ratio::aspect_ratio_container; use cosmic::widget::nav_bar::nav_bar_style; use cosmic::widget::tooltip::Position as TPosition; use cosmic::widget::{ - button, context_drawer, image, nav_bar, text, tooltip, + button, context_drawer, image, nav_bar, text, tooltip, MouseArea, Responsive, Space, }; use cosmic::widget::{icon, slider}; @@ -94,6 +93,8 @@ struct App { current_slide: Slide, presentation_open: bool, cli_mode: bool, + library_open: bool, + library_width: f32, } #[derive(Debug, Clone)] @@ -107,6 +108,7 @@ enum Message { WindowOpened(window::Id, Option), WindowClosed(window::Id), Quit, + Key(Key, Modifiers), None, } @@ -180,6 +182,8 @@ impl cosmic::Application for App { current_slide, presentation_open: false, cli_mode: !input.ui, + library_open: true, + library_width: 60.0, }; let command; @@ -300,75 +304,44 @@ impl cosmic::Application for App { Some(text::body("Sux").into()) } - fn subscription( - &self, - ) -> cosmic::iced_futures::Subscription { + fn subscription(&self) -> Subscription { event::listen_with(|event, _, id| { - if let iced::Event::Window(window_event) = event { - match window_event { - window::Event::CloseRequested => { - debug!("Closing window"); - Some(Message::CloseWindow(Some(id))) - } - window::Event::Opened { position, .. } => { - debug!(?window_event, ?id); - Some(Message::WindowOpened(id, position)) - } - window::Event::Closed => { - debug!("Closed window"); - Some(Message::WindowClosed(id)) - } - _ => None, - } - } else if let iced::Event::Keyboard(key) = event { - match key { - iced::keyboard::Event::KeyReleased { + // debug!(?event); + match event { + iced::Event::Keyboard(event) => match event { + iced::keyboard::Event::KeyPressed { key, - modified_key: _, - physical_key: _, - location: _, - modifiers: _, - } => match key { - Key::Named( - iced::keyboard::key::Named::ArrowRight, - ) => Some(Message::Present( - ui::presenter::Message::NextSlide, - )), - Key::Named( - iced::keyboard::key::Named::ArrowLeft, - ) => Some(Message::Present( - ui::presenter::Message::NextSlide, - )), - Key::Named( - iced::keyboard::key::Named::Space, - ) => Some(Message::Present( - ui::presenter::Message::NextSlide, - )), - Key::Character(key) => { - if key == SmolStr::from("j") - || key == SmolStr::from("l") - { - Some(Message::Present( - ui::presenter::Message::NextSlide, - )) - } else if key == SmolStr::from("k") - || key == SmolStr::from("h") - { - Some(Message::Present( - ui::presenter::Message::PrevSlide, - )) - } else if key == SmolStr::from("q") { - Some(Message::Quit) - } else { - None - } + modifiers, + .. + } => Some(Message::Key(key, modifiers)), + _ => None, + }, + iced::Event::Mouse(event) => None, + iced::Event::Window(window_event) => { + match window_event { + window::Event::CloseRequested => { + debug!("Closing window"); + Some(Message::CloseWindow(Some(id))) + } + window::Event::Opened { + position, .. + } => { + debug!(?window_event, ?id); + Some(Message::WindowOpened(id, position)) + } + window::Event::Closed => { + debug!("Closed window"); + Some(Message::WindowClosed(id)) } _ => None, - }, - _ => None, + } + } + iced::Event::Touch(event) => None, + iced::Event::A11y(id, action_request) => None, + iced::Event::Dnd(dnd_event) => None, + iced::Event::PlatformSpecific(platform_specific) => { + None } - } else { - None } }) } @@ -391,8 +364,57 @@ impl cosmic::Application for App { fn update(&mut self, message: Message) -> Task { match message { + Message::Key(key, modifiers) => { + debug!(?key, ?modifiers); + match (key, modifiers) { + ( + Key::Named( + iced::keyboard::key::Named::ArrowRight, + ), + _, + ) => self.update(Message::Present( + presenter::Message::NextSlide, + )), + ( + Key::Named( + iced::keyboard::key::Named::ArrowLeft, + ), + _, + ) => self.update(Message::Present( + presenter::Message::PrevSlide, + )), + ( + Key::Named(iced::keyboard::key::Named::Space), + _, + ) => self.update(Message::Present( + presenter::Message::NextSlide, + )), + (Key::Character(k), _) + if k == SmolStr::from("j") + || k == SmolStr::from("l") => + { + self.update(Message::Present( + presenter::Message::NextSlide, + )) + } + (Key::Character(k), _) + if k == SmolStr::from("k") + || k == SmolStr::from("h") => + { + self.update(Message::Present( + presenter::Message::PrevSlide, + )) + } + (Key::Character(k), _) + if k == SmolStr::from("q") => + { + self.update(Message::Quit) + } + _ => Task::none(), + } + } Message::Present(message) => { - debug!(?message); + // debug!(?message); if self.presentation_open { if let Some(video) = &mut self.presenter.video { video.set_muted(false); @@ -550,6 +572,14 @@ impl cosmic::Application for App { ] .spacing(3); + // let library = Container::new("library") + // .center(Length::Fill) + // .width(self.library_width); + // let drag_handle = Container::new(Space::new(1, Length::Fill)) + // .style(|t| nav_bar_style(t)); + // let dragger = MouseArea::new(drag_handle) + // .on_drag(Message::LibraryWidth); + let row = row![ Container::new( button::icon(icon_left) diff --git a/src/ui/presenter.rs b/src/ui/presenter.rs index 0691f66..1c1b5b7 100644 --- a/src/ui/presenter.rs +++ b/src/ui/presenter.rs @@ -1,6 +1,4 @@ -use std::{ - fs::File, io::BufReader, path::PathBuf, sync::Arc, thread, -}; +use std::{fs::File, io::BufReader, path::PathBuf, sync::Arc}; use cosmic::{ dialog::ashpd::url::Url, @@ -12,8 +10,7 @@ use cosmic::{ }, iced_widget::{ scrollable::{ - scroll_by, scroll_to, AbsoluteOffset, Direction, - Scrollbar, + scroll_to, AbsoluteOffset, Direction, Scrollbar, }, stack, }, @@ -45,6 +42,7 @@ pub(crate) struct Presenter { sink: (OutputStream, Arc), hovered_slide: i32, scroll_id: Id, + current_font: Font, } #[derive(Debug, Clone, PartialEq)] @@ -59,6 +57,7 @@ pub(crate) enum Message { VideoPos(f32), VideoFrame, HoveredSlide(i32), + ChangeFont(String), None, } @@ -109,6 +108,7 @@ impl Presenter { ) }, scroll_id: Id::unique(), + current_font: cosmic::font::default(), } } @@ -138,10 +138,11 @@ impl Presenter { } Message::SlideChange(id) => { debug!(id, "slide changed"); - let right = self.current_slide_index < id; self.current_slide_index = id; if let Some(slide) = self.slides.get(id as usize) { self.current_slide = slide.clone(); + let _ = self + .update(Message::ChangeFont(slide.font())); } if let Some(video) = &mut self.video { let _ = video.restart_stream(); @@ -196,6 +197,21 @@ impl Presenter { } Task::batch(tasks) } + Message::ChangeFont(s) => { + let font_name = s.into_boxed_str(); + let family = Family::Name(Box::leak(font_name)); + let weight = Weight::Normal; + let stretch = Stretch::Normal; + let style = Style::Normal; + let font = Font { + family, + weight, + stretch, + style, + }; + self.current_font = font; + Task::none() + } Message::EndVideo => { // if self.current_slide.video_loop() { // if let Some(video) = &mut self.video { @@ -266,17 +282,7 @@ impl Presenter { pub fn view(&self) -> Element { responsive(|size| { - let font = self.current_slide.font().into_boxed_str(); - let family = Family::Name(Box::leak(font)); - let weight = Weight::Normal; - let stretch = Stretch::Normal; - let style = Style::Normal; - let font = Font { - family, - weight, - stretch, - style, - }; + let font = self.current_font; let font_size = if self.current_slide.font_size() > 0 { (size.width / self.current_slide.font_size() as f32) * 3.0 @@ -341,6 +347,7 @@ impl Presenter { if let Some(video) = &self.video { Container::new( VideoPlayer::new(video) + .mouse_hidden(true) .width(size.width) .height(size.width * 9.0 / 16.0) .on_end_of_stream(Message::EndVideo) @@ -366,17 +373,7 @@ impl Presenter { pub fn view_preview(&self) -> Element { responsive(|size| { - let font = self.current_slide.font().into_boxed_str(); - let family = Family::Name(Box::leak(font)); - let weight = Weight::Normal; - let stretch = Stretch::Normal; - let style = Style::Normal; - let font = Font { - family, - weight, - stretch, - style, - }; + let font = self.current_font; let font_size = if self.current_slide.font_size() > 0 { (size.width / self.current_slide.font_size() as f32) * 3.0 @@ -448,7 +445,8 @@ impl Presenter { &'a self, slide: &'a Slide, ) -> Element<'a, Message> { - let family = Family::Name("VictorMono Nerd Font"); + let font_name = slide.font().into_boxed_str(); + let family = Family::Name(Box::leak(font_name)); let weight = Weight::Normal; let stretch = Stretch::Normal; let style = Style::Normal;