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::widget::{Column, TextInput, text};
|
||||||
use iced::{Alignment, Color, Element, Event, Length, Task as Command, event};
|
use iced::{Color, Element, Event, Task as Command, event};
|
||||||
use iced_layershell::application;
|
use iced_layershell::application;
|
||||||
use iced_layershell::reexport::Anchor;
|
use iced_layershell::reexport::Anchor;
|
||||||
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
|
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
|
||||||
|
|
@ -12,7 +12,7 @@ pub fn main() -> Result<(), iced_layershell::Error> {
|
||||||
None => StartMode::Active,
|
None => StartMode::Active,
|
||||||
};
|
};
|
||||||
|
|
||||||
application(Counter::default, namespace, update, view)
|
application(State::default, namespace, update, view)
|
||||||
.style(style)
|
.style(style)
|
||||||
.subscription(subscription)
|
.subscription(subscription)
|
||||||
.settings(Settings {
|
.settings(Settings {
|
||||||
|
|
@ -29,16 +29,15 @@ pub fn main() -> Result<(), iced_layershell::Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Counter {
|
struct State {
|
||||||
value: i32,
|
current_message: String,
|
||||||
text: String,
|
history: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[to_layer_message]
|
#[to_layer_message]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
IncrementPressed,
|
TextSubmit,
|
||||||
DecrementPressed,
|
|
||||||
TextInput(String),
|
TextInput(String),
|
||||||
IcedEvent(Event),
|
IcedEvent(Event),
|
||||||
}
|
}
|
||||||
|
|
@ -47,47 +46,55 @@ fn namespace() -> String {
|
||||||
String::from("Counter - Iced")
|
String::from("Counter - Iced")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(_: &Counter) -> iced::Subscription<Message> {
|
fn subscription(_: &State) -> iced::Subscription<Message> {
|
||||||
event::listen().map(Message::IcedEvent)
|
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 {
|
match message {
|
||||||
Message::IcedEvent(event) => {
|
Message::IcedEvent(event) => {
|
||||||
println!("hello {event:?}");
|
if let Event::Keyboard(KeyReleased {
|
||||||
Command::none()
|
key: Key::Named(Named::Escape),
|
||||||
|
..
|
||||||
|
}) = event
|
||||||
|
{
|
||||||
|
return iced::exit();
|
||||||
}
|
}
|
||||||
Message::IncrementPressed => {
|
|
||||||
counter.value += 1;
|
|
||||||
Command::none()
|
|
||||||
}
|
|
||||||
Message::DecrementPressed => {
|
|
||||||
counter.value -= 1;
|
|
||||||
Command::none()
|
Command::none()
|
||||||
}
|
}
|
||||||
Message::TextInput(text) => {
|
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()
|
Command::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unreachable!(),
|
_ => Command::none(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(counter: &Counter) -> Element<'_, Message> {
|
fn view(state: &State) -> Column<'_, Message> {
|
||||||
column![
|
let input: TextInput<'_, Message> =
|
||||||
button("Increment").on_press(Message::IncrementPressed),
|
TextInput::new("Type something here...", &state.current_message)
|
||||||
text(counter.value).size(50),
|
.on_input(Message::TextInput)
|
||||||
button("Decrement").on_press(Message::DecrementPressed)
|
.on_submit(Message::TextSubmit);
|
||||||
]
|
|
||||||
.align_x(Alignment::Center)
|
Column::with_children(
|
||||||
.padding(20)
|
std::iter::once(input.into())
|
||||||
.width(Length::Fill)
|
.chain(state.history.iter().rev().map(|msg| text!("{msg}").into()))
|
||||||
.height(Length::Fill)
|
.collect::<Vec<Element<_>>>(),
|
||||||
.into()
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style(_counter: &Counter, theme: &iced::Theme) -> iced::theme::Style {
|
fn style(_counter: &State, theme: &iced::Theme) -> iced::theme::Style {
|
||||||
use iced::theme::Style;
|
use iced::theme::Style;
|
||||||
Style {
|
Style {
|
||||||
background_color: Color::WHITE,
|
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