forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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,17 @@ | ||
[package] | ||
name = "sctk_custom_output" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
iced = { path = "../..", features = [ | ||
"debug", | ||
"lazy", | ||
"wayland", | ||
"winit", | ||
"tokio", | ||
"tiny-skia", | ||
"advanced", | ||
], default-features = false } | ||
tokio = { workspace = true, features = ["time"] } |
This file contains 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::env; | ||
use std::time::Duration; | ||
|
||
use iced::platform_specific::shell::commands::layer_surface::get_layer_surface; | ||
use iced::platform_specific::shell::commands::output::{ | ||
get_output, get_output_info, | ||
}; | ||
use iced::runtime::platform_specific::wayland::layer_surface::SctkLayerSurfaceSettings; | ||
use iced::widget::text; | ||
use iced::{daemon, stream}; | ||
use iced::{ | ||
platform_specific::shell::commands::output::OutputInfo, | ||
runtime::platform_specific::wayland::layer_surface::IcedOutput, Element, | ||
Task, | ||
}; | ||
|
||
use iced::window::Id; | ||
use tokio::time::sleep; | ||
|
||
fn main() -> iced::Result { | ||
daemon("Custom Output", App::update, App::view).run_with(App::new) | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Message { | ||
Output(Option<IcedOutput>), | ||
OutputInfo(Option<OutputInfo>), | ||
} | ||
|
||
#[derive(Debug)] | ||
struct App { | ||
monitor: Option<String>, | ||
output: IcedOutput, | ||
logical_size: Option<(i32, i32)>, | ||
} | ||
|
||
impl App { | ||
fn new() -> (App, Task<Message>) { | ||
let app = App { | ||
monitor: env::var("WL_OUTPUT").ok(), | ||
output: IcedOutput::Active, | ||
logical_size: None, | ||
}; | ||
|
||
let task = match &app.monitor { | ||
Some(_) => app.try_get_output(), | ||
None => app.open(), | ||
}; | ||
|
||
(app, task) | ||
} | ||
|
||
fn try_get_output(&self) -> Task<Message> { | ||
let monitor = self.monitor.clone(); | ||
get_output(move |output_state| { | ||
output_state | ||
.outputs() | ||
.find(|o| { | ||
output_state | ||
.info(o) | ||
.map(|info| info.name == monitor) | ||
.unwrap_or(false) | ||
}) | ||
.clone() | ||
}) | ||
.map(|optn| Message::Output(optn.map(IcedOutput::Output))) | ||
} | ||
|
||
fn try_get_output_info(&self) -> Task<Message> { | ||
let monitor = self.monitor.clone(); | ||
get_output_info(move |output_state| { | ||
output_state | ||
.outputs() | ||
.find(|o| { | ||
output_state | ||
.info(o) | ||
.map(|info| info.name == monitor) | ||
.unwrap_or(false) | ||
}) | ||
.and_then(|o| output_state.info(&o)) | ||
.clone() | ||
}) | ||
.map(Message::OutputInfo) | ||
} | ||
|
||
fn open(&self) -> Task<Message> { | ||
get_layer_surface(SctkLayerSurfaceSettings { | ||
output: self.output.clone(), | ||
size: match self.logical_size { | ||
Some(size) => { | ||
Some((Some((size.0 / 2) as u32), Some((size.1 / 2) as u32))) | ||
} | ||
None => Some((Some(800), Some(500))), | ||
}, | ||
..Default::default() | ||
}) | ||
} | ||
|
||
fn update(&mut self, msg: Message) -> Task<Message> { | ||
match msg { | ||
Message::Output(optn) => match optn { | ||
Some(output) => { | ||
self.output = output; | ||
self.try_get_output_info() | ||
} | ||
None => Task::stream(stream::channel(1, |_| async { | ||
sleep(Duration::from_millis(500)).await; | ||
})) | ||
.chain(self.try_get_output()), | ||
}, | ||
Message::OutputInfo(optn) => match optn { | ||
Some(info) => { | ||
self.logical_size = info.logical_size; | ||
self.open() | ||
} | ||
None => Task::stream(stream::channel(1, |_| async { | ||
sleep(Duration::from_millis(500)).await; | ||
})) | ||
.chain(self.try_get_output_info()), | ||
}, | ||
} | ||
} | ||
|
||
fn view(&self, _window_id: Id) -> Element<Message> { | ||
match &self.monitor { | ||
Some(monitor) => text!("Opened on monitor {monitor}\nSize: {:?}", self.logical_size), | ||
None => text!("No output specified, try setting WL_OUTPUT=YourMonitor\nDefaulting to size 800x500"), | ||
}.into() | ||
} | ||
} |
This file contains 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 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 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 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,30 @@ | ||
use std::fmt; | ||
|
||
use cctk::{ | ||
sctk::output::{OutputInfo, OutputState}, | ||
wayland_client::protocol::wl_output::WlOutput, | ||
}; | ||
|
||
use crate::oneshot; | ||
|
||
pub enum Action { | ||
// WlOutput getter | ||
GetOutput { | ||
f: Box<dyn Fn(&OutputState) -> Option<WlOutput> + Send + Sync>, | ||
channel: oneshot::Sender<Option<WlOutput>>, | ||
}, | ||
// OutputInfo getter | ||
GetOutputInfo { | ||
f: Box<dyn Fn(&OutputState) -> Option<OutputInfo> + Send + Sync>, | ||
channel: oneshot::Sender<Option<OutputInfo>>, | ||
}, | ||
} | ||
|
||
impl fmt::Debug for Action { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Action::GetOutput { .. } => write!(f, "Action::GetOutput"), | ||
Action::GetOutputInfo { .. } => write!(f, "Action::GetOutputInfo",), | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,25 +1,40 @@ | ||
use cctk::sctk::output::{OutputInfo, OutputState}; | ||
use iced_runtime::{platform_specific::{self, wayland}, task, Action, Task}; | ||
use wayland_client::protocol::wl_output::WlOutput; | ||
pub use cctk::sctk::output::{OutputInfo, OutputState}; | ||
use iced_runtime::{ | ||
platform_specific::{self, wayland}, | ||
task, Action, Task, | ||
}; | ||
pub use wayland_client::protocol::wl_output::WlOutput; | ||
|
||
pub fn get_output<F>(f: F) -> Task<Option<WlOutput>> where F: Fn(&OutputState) -> Option<WlOutput> + Send + Sync + 'static { | ||
task::oneshot(|channel| Action::PlatformSpecific( | ||
platform_specific::Action::Wayland( | ||
wayland::Action::GetOutput { | ||
/// Get a | ||
/// [WlOutput](https://docs.rs/wayland-client/latest/wayland_client/protocol/wl_output/struct.WlOutput.html) by calling a closure on a | ||
/// [&OutputState](https://docs.rs/smithay-client-toolkit/latest/smithay_client_toolkit/output/struct.OutputState.html) | ||
pub fn get_output<F>(f: F) -> Task<Option<WlOutput>> | ||
where | ||
F: Fn(&OutputState) -> Option<WlOutput> + Send + Sync + 'static, | ||
{ | ||
task::oneshot(|channel| { | ||
Action::PlatformSpecific(platform_specific::Action::Wayland( | ||
wayland::Action::Output(wayland::output::Action::GetOutput { | ||
f: Box::new(f), | ||
channel, | ||
} | ||
) | ||
)) | ||
}), | ||
)) | ||
}) | ||
} | ||
|
||
pub fn get_output_info<F>(f: F) -> Task<Option<OutputInfo>> where F: Fn(&OutputState) -> Option<OutputInfo> + Send + Sync + 'static { | ||
task::oneshot(|channel| Action::PlatformSpecific( | ||
platform_specific::Action::Wayland( | ||
wayland::Action::GetOutputInfo { | ||
/// Get a | ||
/// [OutputInfo](https://docs.rs/smithay-client-toolkit/latest/smithay_client_toolkit/output/struct.OutputInfo.html) by calling a closure on a | ||
/// [&OutputState](https://docs.rs/smithay-client-toolkit/latest/smithay_client_toolkit/output/struct.OutputState.html) | ||
pub fn get_output_info<F>(f: F) -> Task<Option<OutputInfo>> | ||
where | ||
F: Fn(&OutputState) -> Option<OutputInfo> + Send + Sync + 'static, | ||
{ | ||
task::oneshot(|channel| { | ||
Action::PlatformSpecific(platform_specific::Action::Wayland( | ||
wayland::Action::Output(wayland::output::Action::GetOutputInfo { | ||
f: Box::new(f), | ||
channel, | ||
} | ||
) | ||
)) | ||
}), | ||
)) | ||
}) | ||
} |
This file contains 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