better init

This commit is contained in:
maxstrb 2026-02-26 22:49:56 +01:00
parent 4bdfce0ad1
commit b0dd7b0004

View file

@ -42,7 +42,6 @@ pub fn main() -> Result<(), iced_layershell::Error> {
} }
struct State { struct State {
initialized: bool,
current_message: String, current_message: String,
history: Vec<History>, history: Vec<History>,
input_id: Id, input_id: Id,
@ -62,7 +61,6 @@ impl History {
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self { Self {
initialized: false,
current_message: String::new(), current_message: String::new(),
history: Vec::new(), history: Vec::new(),
input_id: Id::unique(), input_id: Id::unique(),
@ -83,27 +81,21 @@ fn namespace() -> String {
String::from("Iced qalc claculator") String::from("Iced qalc claculator")
} }
fn subscription(state: &State) -> iced::Subscription<Message> { fn subscription(_state: &State) -> iced::Subscription<Message> {
use iced::event; use iced::event;
use iced::futures::stream; use iced::futures::stream;
let init = if !state.initialized { iced::Subscription::batch([
iced::Subscription::run_with("init", |_| stream::once(async { Message::Init })) iced::Subscription::run_with("init", |_| stream::once(async { Message::Init })),
} else { event::listen().map(Message::IcedEvent),
iced::Subscription::none() ])
};
iced::Subscription::batch([init, event::listen().map(Message::IcedEvent)])
} }
fn update(state: &mut State, message: Message) -> Command<Message> { fn update(state: &mut State, message: Message) -> Command<Message> {
use iced::keyboard::{Event::KeyReleased, Key, key::Named}; use iced::keyboard::{Event::KeyReleased, Key, key::Named};
match message { match message {
Message::Init => { Message::Init => operation::focus(state.input_id.clone()),
state.initialized = true;
operation::focus(state.input_id.clone())
}
Message::IcedEvent(event) => { Message::IcedEvent(event) => {
if let Event::Keyboard(KeyReleased { if let Event::Keyboard(KeyReleased {
key: Key::Named(Named::Escape), key: Key::Named(Named::Escape),