Skip to content

Commit

Permalink
Added example
Browse files Browse the repository at this point in the history
  • Loading branch information
manthanabc committed Dec 28, 2024
1 parent f7b4d7c commit 8980620
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 10 deletions.
79 changes: 79 additions & 0 deletions iced_layershell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,83 @@ impl Counter {
```

### Dynamically set input region
```rust, no_run
use iced::widget::{button, row, Space};
use iced::{Color, Element, Length, Task as Command, Theme};
use iced_layershell::settings::{LayerShellSettings, Settings};
use iced_layershell::to_layer_message;
use iced_layershell::Application;
pub fn main() -> Result<(), iced_layershell::Error> {
InputRegionExample::run(Settings {
layer_settings: LayerShellSettings {
size: Some((400, 400)),
..Default::default()
},
..Default::default()
})
}
struct InputRegionExample;
#[to_layer_message]
#[derive(Debug, Clone)]
#[doc = "Some docs"]
enum Message {
SetRegion,
UnsetRegion,
}
impl Application for InputRegionExample {
type Message = Message;
type Flags = ();
type Theme = Theme;
type Executor = iced::executor::Default;
fn new(_flags: ()) -> (Self, Command<Message>) {
(Self, Command::none())
}
fn namespace(&self) -> String {
String::from("Counter - Iced")
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::SetRegion => Command::done(Message::SetInputRegion(|region| {
// Only the buttons!
region.add(0, 0, 400, 70);
})),
Message::UnsetRegion => Command::done(Message::SetInputRegion(|region| {
// Entire window!
region.add(0, 0, 400, 400);
})),
_ => unreachable!(),
}
}
fn view(&self) -> Element<Message> {
// Create the top row with two buttons
row![
button("Set region").on_press(Message::SetRegion),
Space::with_width(Length::Fill),
button("Reset region").on_press(Message::UnsetRegion),
]
.padding(20)
.spacing(10)
.width(Length::Fill)
.into()
}
fn style(&self, theme: &Self::Theme) -> iced_layershell::Appearance {
use iced_layershell::Appearance;
Appearance {
background_color: Color::from_rgba(0.3, 0.3, 0.3, 0.3),
text_color: theme.palette().text,
}
}
}
```
For more example, please take a look at [exwlshelleventloop](https://github.com/waycrate/exwlshelleventloop)
17 changes: 7 additions & 10 deletions iced_layershell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,15 @@ where
LayershellCustomActions::SetInputRegion(set_region) => {
let window = ev.main_window();

if let Some(region) = &wl_input_region {
let window_size = window.get_size();
let width: i32 = window_size.0.try_into().unwrap_or_default();
let height: i32 = window_size.1.try_into().unwrap_or_default();
let region = wl_input_region.as_ref().expect("region not found");
let window_size = window.get_size();
let width: i32 = window_size.0.try_into().unwrap_or_default();
let height: i32 = window_size.1.try_into().unwrap_or_default();

region.subtract(0, 0, width, height);
set_region(region);
}
region.subtract(0, 0, width, height);
set_region(&region);

window
.get_wlsurface()
.set_input_region(wl_input_region.as_ref());
window.get_wlsurface().set_input_region(Some(&region));
}
LayershellCustomActions::MarginChange(margin) => {
ev.main_window().set_margin(margin);
Expand Down

0 comments on commit 8980620

Please sign in to comment.