Wlr layer with proper exit and text_input
This commit is contained in:
parent
a831c881a3
commit
ccd64d35f7
2 changed files with 40 additions and 83 deletions
71
src/main.rs
71
src/main.rs
|
|
@ -1,5 +1,5 @@
|
|||
use iced::widget::{button, column, row, text, text_input};
|
||||
use iced::{Alignment, Color, Element, Event, Length, Task as Command, event};
|
||||
use iced::widget::{Column, TextInput, text};
|
||||
use iced::{Color, Element, Event, Task as Command, event};
|
||||
use iced_layershell::application;
|
||||
use iced_layershell::reexport::Anchor;
|
||||
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
|
||||
|
|
@ -12,7 +12,7 @@ pub fn main() -> Result<(), iced_layershell::Error> {
|
|||
None => StartMode::Active,
|
||||
};
|
||||
|
||||
application(Counter::default, namespace, update, view)
|
||||
application(State::default, namespace, update, view)
|
||||
.style(style)
|
||||
.subscription(subscription)
|
||||
.settings(Settings {
|
||||
|
|
@ -29,16 +29,15 @@ pub fn main() -> Result<(), iced_layershell::Error> {
|
|||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Counter {
|
||||
value: i32,
|
||||
text: String,
|
||||
struct State {
|
||||
current_message: String,
|
||||
history: Vec<String>,
|
||||
}
|
||||
|
||||
#[to_layer_message]
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
IncrementPressed,
|
||||
DecrementPressed,
|
||||
TextSubmit,
|
||||
TextInput(String),
|
||||
IcedEvent(Event),
|
||||
}
|
||||
|
|
@ -47,47 +46,55 @@ fn namespace() -> String {
|
|||
String::from("Counter - Iced")
|
||||
}
|
||||
|
||||
fn subscription(_: &Counter) -> iced::Subscription<Message> {
|
||||
fn subscription(_: &State) -> iced::Subscription<Message> {
|
||||
event::listen().map(Message::IcedEvent)
|
||||
}
|
||||
|
||||
fn update(counter: &mut Counter, message: Message) -> Command<Message> {
|
||||
fn update(state: &mut State, message: Message) -> Command<Message> {
|
||||
use iced::keyboard::{Event::KeyReleased, Key, key::Named};
|
||||
|
||||
match message {
|
||||
Message::IcedEvent(event) => {
|
||||
println!("hello {event:?}");
|
||||
Command::none()
|
||||
if let Event::Keyboard(KeyReleased {
|
||||
key: Key::Named(Named::Escape),
|
||||
..
|
||||
}) = event
|
||||
{
|
||||
return iced::exit();
|
||||
}
|
||||
Message::IncrementPressed => {
|
||||
counter.value += 1;
|
||||
Command::none()
|
||||
}
|
||||
Message::DecrementPressed => {
|
||||
counter.value -= 1;
|
||||
|
||||
Command::none()
|
||||
}
|
||||
Message::TextInput(text) => {
|
||||
counter.text = text;
|
||||
state.current_message = text;
|
||||
Command::none()
|
||||
}
|
||||
Message::TextSubmit => {
|
||||
state
|
||||
.history
|
||||
.push(std::mem::take(&mut state.current_message));
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
||||
_ => unreachable!(),
|
||||
_ => Command::none(),
|
||||
}
|
||||
}
|
||||
|
||||
fn view(counter: &Counter) -> Element<'_, Message> {
|
||||
column![
|
||||
button("Increment").on_press(Message::IncrementPressed),
|
||||
text(counter.value).size(50),
|
||||
button("Decrement").on_press(Message::DecrementPressed)
|
||||
]
|
||||
.align_x(Alignment::Center)
|
||||
.padding(20)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.into()
|
||||
fn view(state: &State) -> Column<'_, Message> {
|
||||
let input: TextInput<'_, Message> =
|
||||
TextInput::new("Type something here...", &state.current_message)
|
||||
.on_input(Message::TextInput)
|
||||
.on_submit(Message::TextSubmit);
|
||||
|
||||
Column::with_children(
|
||||
std::iter::once(input.into())
|
||||
.chain(state.history.iter().rev().map(|msg| text!("{msg}").into()))
|
||||
.collect::<Vec<Element<_>>>(),
|
||||
)
|
||||
}
|
||||
|
||||
fn style(_counter: &Counter, theme: &iced::Theme) -> iced::theme::Style {
|
||||
fn style(_counter: &State, theme: &iced::Theme) -> iced::theme::Style {
|
||||
use iced::theme::Style;
|
||||
Style {
|
||||
background_color: Color::WHITE,
|
||||
|
|
|
|||
|
|
@ -1,50 +0,0 @@
|
|||
use iced::{
|
||||
Element,
|
||||
widget::{Column, TextInput, text},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
current_input: String,
|
||||
old_messages: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
ContentChanged(String),
|
||||
NewMessage,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::ContentChanged(content) => {
|
||||
self.current_input = content;
|
||||
}
|
||||
|
||||
Message::NewMessage => {
|
||||
self.old_messages
|
||||
.push(std::mem::take(&mut self.current_input));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn view(&self) -> Column<'_, Message> {
|
||||
let input: TextInput<'_, Message> =
|
||||
TextInput::new("Type something here...", &self.current_input)
|
||||
.on_input(Message::ContentChanged)
|
||||
.on_submit(Message::NewMessage);
|
||||
|
||||
Column::with_children(
|
||||
std::iter::once(input.into())
|
||||
.chain(self.old_messages.iter().map(|msg| text!("{msg}").into()))
|
||||
.collect::<Vec<Element<_>>>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() -> iced::Result {
|
||||
iced::run(State::update, State::view)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue