-
-
Notifications
You must be signed in to change notification settings - Fork 10
Add GUI Area Selector #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nicolo-mn
wants to merge
18
commits into
waycrate:master
Choose a base branch
from
nicolo-mn:gui-selector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b605fdd
feat: add gui selector
nicolo-mn e498724
fix: handle missing screenshots (due to, for example, unsupported top…
nicolo-mn f662b79
chore: remove unused temporary function, fix formatting
nicolo-mn 399ec7f
refactor: naming and comments
nicolo-mn 7787670
fix: add missing conflicts with -g flag
nicolo-mn a5d9710
chore: adapt wayshot calls after changes to its main branch
nicolo-mn dacc4db
feat: make screenshots lazy
nicolo-mn 250a851
chore: make gui feature selectable
nicolo-mn 382490d
feat: add i18n
nicolo-mn 48fff82
feat: make AreaSelectorGUI accept a WayshotConnection
nicolo-mn ccace0a
chore: add example
nicolo-mn 6cafcfd
build: switch iced_layershell and libwayshot dependencies to stable r…
nicolo-mn e1f3265
chore: change namespace id to be the same as slurp
nicolo-mn 461d944
feat: add theme choice
nicolo-mn a647591
chore: add ArgGroups to avoid repetitions
nicolo-mn 2998841
chore: add debug feature (avoids including rust-embed/debug-embed in …
nicolo-mn 1b369ce
chore: start GUI selector on active monitor
nicolo-mn 3c494e9
chore: move from daemon to application
nicolo-mn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| screens = Screens | ||
| windows = Windows | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use libwayshot::WayshotConnection; | ||
| use libwaysip::gui_selector::{AreaSelectorGUI, GUISelection}; | ||
| use wayland_client::Connection; | ||
|
|
||
| fn main() { | ||
| let connection = | ||
| WayshotConnection::from_connection(Connection::connect_to_env().unwrap()).unwrap(); | ||
|
|
||
| let selector = AreaSelectorGUI::new().with_connection(connection); | ||
| match selector.launch() { | ||
| GUISelection::Output(output) => println!( | ||
| "Selected output with title {} and positioned in {}", | ||
| output.name, output.logical_region | ||
| ), | ||
| GUISelection::Toplevel(toplevel) => println!( | ||
| "Selected toplevel with title {} and app_id {}", | ||
| toplevel.title, toplevel.app_id | ||
| ), | ||
| GUISelection::Failed => println!("GUI selection failed!"), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| fallback_language = "en-US" | ||
|
|
||
| [fluent] | ||
| assets_dir = "assets/locales" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| use std::sync::{Arc, mpsc}; | ||
| use std::time::Duration; | ||
|
|
||
| use clap::ValueEnum; | ||
| use iced::Theme; | ||
| use iced_layershell::application; | ||
| use iced_layershell::reexport::{Anchor, KeyboardInteractivity}; | ||
| use iced_layershell::settings::{LayerShellSettings, Settings}; | ||
| use libwayshot::WayshotConnection; | ||
| use libwayshot::output::OutputInfo; | ||
| use libwayshot::region::TopLevel; | ||
|
|
||
| use crate::iced_selector::IcedSelector; | ||
|
|
||
| // ValueEnum allows client code to use clap for the theme selection | ||
| #[derive(ValueEnum, Clone, Default)] | ||
| pub enum WaySipTheme { | ||
| Light, | ||
| #[default] | ||
| Dark, | ||
| Dracula, | ||
| Nord, | ||
| SolarizedLight, | ||
| SolarizedDark, | ||
| GruvboxLight, | ||
| GruvboxDark, | ||
| CatppuccinLatte, | ||
| CatppuccinFrappe, | ||
| CatppuccinMacchiato, | ||
| CatppuccinMocha, | ||
| TokyoNight, | ||
| TokyoNightStorm, | ||
| TokyoNightLight, | ||
| KanagawaWave, | ||
| KanagawaDragon, | ||
| KanagawaLotus, | ||
| Moonfly, | ||
| Oxocarbon, | ||
| Ferra, | ||
| } | ||
|
|
||
| impl From<WaySipTheme> for Theme { | ||
| fn from(theme: WaySipTheme) -> Theme { | ||
| match theme { | ||
| WaySipTheme::Light => Theme::Light, | ||
| WaySipTheme::Dark => Theme::Dark, | ||
| WaySipTheme::Dracula => Theme::Dracula, | ||
| WaySipTheme::Nord => Theme::Nord, | ||
| WaySipTheme::SolarizedLight => Theme::SolarizedLight, | ||
| WaySipTheme::SolarizedDark => Theme::SolarizedDark, | ||
| WaySipTheme::GruvboxLight => Theme::GruvboxLight, | ||
| WaySipTheme::GruvboxDark => Theme::GruvboxDark, | ||
| WaySipTheme::CatppuccinLatte => Theme::CatppuccinLatte, | ||
| WaySipTheme::CatppuccinFrappe => Theme::CatppuccinFrappe, | ||
| WaySipTheme::CatppuccinMacchiato => Theme::CatppuccinMacchiato, | ||
| WaySipTheme::CatppuccinMocha => Theme::CatppuccinMocha, | ||
| WaySipTheme::TokyoNight => Theme::TokyoNight, | ||
| WaySipTheme::TokyoNightStorm => Theme::TokyoNightStorm, | ||
| WaySipTheme::TokyoNightLight => Theme::TokyoNightLight, | ||
| WaySipTheme::KanagawaWave => Theme::KanagawaWave, | ||
| WaySipTheme::KanagawaDragon => Theme::KanagawaDragon, | ||
| WaySipTheme::KanagawaLotus => Theme::KanagawaLotus, | ||
| WaySipTheme::Moonfly => Theme::Moonfly, | ||
| WaySipTheme::Oxocarbon => Theme::Oxocarbon, | ||
| WaySipTheme::Ferra => Theme::Ferra, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Interface struct to start a GUI area selector and retrieve its result | ||
| #[derive(Default)] | ||
| pub struct AreaSelectorGUI { | ||
| conn: Option<WayshotConnection>, | ||
| theme: WaySipTheme, | ||
| } | ||
|
|
||
| /// Represents the user's selection made through interaction with the GUI area selector | ||
| pub enum GUISelection { | ||
| Toplevel(TopLevel), | ||
| Output(OutputInfo), | ||
| Failed, | ||
| } | ||
|
|
||
| impl AreaSelectorGUI { | ||
| pub fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| pub fn with_connection(mut self, conn: WayshotConnection) -> Self { | ||
| self.conn = Some(conn); | ||
| self | ||
| } | ||
|
|
||
| pub fn with_theme(mut self, theme: WaySipTheme) -> Self { | ||
| self.theme = theme; | ||
| self | ||
| } | ||
|
|
||
| /// Launches a GUI area selector | ||
| pub fn launch(self) -> GUISelection { | ||
| let (tx, rx) = mpsc::channel::<GUISelection>(); | ||
| let conn = Arc::new(match self.conn { | ||
| Some(conn) => conn, | ||
| None => WayshotConnection::new().expect("Couldn't establish a Wayshot connection"), | ||
| }); | ||
|
|
||
| let _ = application( | ||
| move || IcedSelector::new(tx.clone(), conn.clone()), | ||
| IcedSelector::namespace, | ||
| IcedSelector::update, | ||
| IcedSelector::view, | ||
| ) | ||
| .settings(Settings { | ||
| layer_settings: LayerShellSettings { | ||
| size: Some((400, 400)), | ||
| exclusive_zone: 0, | ||
| anchor: Anchor::Bottom | Anchor::Left | Anchor::Right | Anchor::Top, | ||
| keyboard_interactivity: KeyboardInteractivity::None, | ||
| ..Default::default() | ||
| }, | ||
| ..Default::default() | ||
| }) | ||
| .theme(Into::<Theme>::into(self.theme)) | ||
| .run(); | ||
|
|
||
| // Gets the selection from the GUI | ||
| rx.recv_timeout(Duration::from_secs(1)) | ||
| .unwrap_or(GUISelection::Failed) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| use std::sync::mpsc::Sender; | ||
|
|
||
| use iced::widget::{Button, Column, button, column, image as iced_image, row, scrollable, text}; | ||
| use iced::{Alignment, Element, Length, Task}; | ||
| use iced_image::Handle; | ||
| use iced_layershell::to_layer_message; | ||
| use iced_runtime::task; | ||
| use libwayshot::WayshotConnection; | ||
| use libwayshot::output::OutputInfo; | ||
| use libwayshot::region::TopLevel; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::fl; | ||
| use crate::gui_selector::GUISelection; | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq)] | ||
| enum ViewMode { | ||
| Screens, | ||
| Windows, | ||
| } | ||
|
|
||
| pub(crate) struct IcedSelector { | ||
| mode: ViewMode, | ||
| toplevels: Vec<(TopLevel, Option<Handle>)>, | ||
| outputs: Vec<(OutputInfo, Option<Handle>)>, | ||
| sender: Sender<GUISelection>, | ||
| } | ||
|
|
||
| #[to_layer_message(multi)] | ||
| #[derive(Debug, Clone)] | ||
| pub(crate) enum Message { | ||
| ShowScreens, | ||
| ShowWindows, | ||
| ScreenSelected(usize), | ||
| WindowSelected(usize), | ||
| OutputScreenshot(usize, Handle), | ||
| ToplevelScreenshot(usize, Handle), | ||
| } | ||
|
|
||
| impl IcedSelector { | ||
| pub(crate) fn new( | ||
| sender: Sender<GUISelection>, | ||
| conn: Arc<WayshotConnection>, | ||
| ) -> (Self, Task<Message>) { | ||
| let toplevels_info = conn.get_all_toplevels().to_vec(); | ||
| let outputs_info = conn.get_all_outputs().to_vec(); | ||
|
|
||
| // Initialize IcedSelector instance with outputs and toplevels obtained | ||
| // through Wayshot, alongside their screenshot (obtained asynchronously) | ||
| let toplevels_tasks = toplevels_info.iter().enumerate().map(|(i, t)| { | ||
| let toplevel = t.clone(); | ||
| let conn = conn.clone(); | ||
| task::blocking(move |sender| { | ||
| // can fail if toplevel capture is not supported | ||
| parse_screenshot_and_send(conn.screenshot_toplevel(&toplevel, false), sender, i); | ||
| }) | ||
| .map(|(i, s)| Message::ToplevelScreenshot(i, s)) | ||
| }); | ||
|
|
||
| let outputs_tasks = outputs_info.iter().enumerate().map(|(i, o)| { | ||
| let output = o.clone(); | ||
| let conn = conn.clone(); | ||
| task::blocking(move |sender| { | ||
| parse_screenshot_and_send(conn.screenshot_single_output(&output, false), sender, i); | ||
| }) | ||
| .map(|(i, s)| Message::OutputScreenshot(i, s)) | ||
| }); | ||
|
|
||
| let all_tasks = Task::batch(toplevels_tasks.chain(outputs_tasks)); | ||
|
|
||
| ( | ||
| Self { | ||
| mode: ViewMode::Screens, | ||
| toplevels: toplevels_info.into_iter().map(|t| (t, None)).collect(), | ||
| outputs: outputs_info.into_iter().map(|o| (o, None)).collect(), | ||
| sender, | ||
| }, | ||
| all_tasks, | ||
| ) | ||
| } | ||
|
|
||
| pub(crate) fn namespace() -> String { | ||
| String::from("selection") // same as slurp | ||
| } | ||
|
|
||
| pub(crate) fn update(&mut self, message: Message) -> Task<Message> { | ||
| match message { | ||
| Message::ShowScreens => { | ||
| self.mode = ViewMode::Screens; | ||
| Task::none() | ||
| } | ||
| Message::ShowWindows => { | ||
| self.mode = ViewMode::Windows; | ||
| Task::none() | ||
| } | ||
| Message::ScreenSelected(index) => { | ||
| let _ = self | ||
| .sender | ||
| .send(GUISelection::Output(self.outputs[index].0.clone())); | ||
| iced::exit() | ||
| } | ||
| Message::WindowSelected(index) => { | ||
| let _ = self | ||
| .sender | ||
| .send(GUISelection::Toplevel(self.toplevels[index].0.clone())); | ||
| iced::exit() | ||
| } | ||
| Message::OutputScreenshot(index, screenshot_handle) => { | ||
| self.outputs[index].1 = Some(screenshot_handle); | ||
| Task::none() | ||
| } | ||
| Message::ToplevelScreenshot(index, screenshot_handle) => { | ||
| self.toplevels[index].1 = Some(screenshot_handle); | ||
| Task::none() | ||
| } | ||
| _ => Task::none(), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn view(&self) -> Element<'_, Message> { | ||
| let selector = row![ | ||
| button(text(fl!("screens")).center().width(Length::Fill)) | ||
| .on_press(Message::ShowScreens) | ||
| .width(Length::Fill) | ||
| .style(if self.mode == ViewMode::Screens { | ||
| button::primary | ||
| } else { | ||
| button::secondary | ||
| }), | ||
| button(text(fl!("windows")).center().width(Length::Fill)) | ||
| .on_press(Message::ShowWindows) | ||
| .width(Length::Fill) | ||
| .style(if self.mode == ViewMode::Windows { | ||
| button::primary | ||
| } else { | ||
| button::secondary | ||
| }), | ||
| ] | ||
| .align_y(Alignment::Center) | ||
| .spacing(10) | ||
| .padding(20) | ||
| .width(Length::Fill); | ||
|
|
||
| let choices: Element<'_, Message> = match self.mode { | ||
| ViewMode::Screens => { | ||
| Column::with_children(self.outputs.iter().enumerate().map(|(i, e)| { | ||
| build_button(e.0.name.clone(), e.1.clone(), Message::ScreenSelected(i)).into() | ||
| })) | ||
| } | ||
| ViewMode::Windows => { | ||
| Column::with_children(self.toplevels.iter().enumerate().map(|(i, e)| { | ||
| build_button(e.0.title.clone(), e.1.clone(), Message::WindowSelected(i)).into() | ||
| })) | ||
| } | ||
| } | ||
| .spacing(10) | ||
| .into(); | ||
|
|
||
| column![selector, scrollable(choices).height(Length::Fill)] | ||
| .padding(20) | ||
| .spacing(10) | ||
| .width(Length::Fill) | ||
| .height(Length::Fill) | ||
| .into() | ||
| } | ||
| } | ||
|
|
||
| fn build_button<'a>( | ||
| label: String, | ||
| screenshot: Option<Handle>, | ||
| message: Message, | ||
| ) -> Button<'a, Message> { | ||
| let button_content: Element<'a, Message> = match screenshot { | ||
| Some(image_handle) => column![ | ||
| text(label).center().width(Length::Fill), | ||
| iced_image(image_handle) | ||
| .width(Length::Fill) | ||
| .height(Length::Fixed(100.0)) | ||
| ] | ||
| .align_x(Alignment::Center) | ||
| .spacing(5) | ||
| .into(), | ||
| _ => text(label).center().width(Length::Fill).into(), | ||
| }; | ||
| button(button_content) | ||
| .on_press(message) | ||
| .width(Length::Fill) | ||
| .style(button::subtle) | ||
| .padding(10) | ||
| } | ||
|
|
||
| fn parse_screenshot_and_send( | ||
| capture: Result<image::DynamicImage, libwayshot::Error>, | ||
| mut sender: futures_channel::mpsc::Sender<(usize, Handle)>, | ||
| elem_index: usize, | ||
| ) { | ||
| if let Ok(screenshot) = capture { | ||
| let rgba_image = screenshot.to_rgba8(); | ||
| let handle = Handle::from_rgba( | ||
| rgba_image.width(), | ||
| rgba_image.height(), | ||
| rgba_image.into_raw(), | ||
| ); | ||
| let _ = sender.try_send((elem_index, handle)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.