examples(application): update and fix compile

This commit is contained in:
Michael Aaron Murphy 2025-12-05 17:05:20 +01:00
parent 8a9cd0da32
commit 2ffd1f32f4
No known key found for this signature in database
GPG key ID: B2732D4240C9212C

View file

@ -3,25 +3,14 @@
//! Application API example //! Application API example
use cosmic::app::Settings;
use cosmic::iced::{Alignment, Length, Size};
use cosmic::widget::menu::{self, KeyBind};
use cosmic::widget::nav_bar;
use cosmic::{executor, iced, prelude::*, widget, Core};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::LazyLock; use std::sync::LazyLock;
use cosmic::app::{Core, Settings, Task};
use cosmic::iced::alignment::{Horizontal, Vertical};
use cosmic::iced::widget::column;
use cosmic::iced::Length;
use cosmic::iced_core::Size;
use cosmic::widget::icon::{from_name, Handle};
use cosmic::widget::menu::KeyBind;
use cosmic::widget::{button, text};
use cosmic::widget::{
container,
menu::menu_button,
menu::{self, action::MenuAction},
nav_bar, responsive,
};
use cosmic::{executor, iced, ApplicationExt, Element};
static MENU_ID: LazyLock<iced::id::Id> = LazyLock::new(|| iced::id::Id::new("menu_id")); static MENU_ID: LazyLock<iced::id::Id> = LazyLock::new(|| iced::id::Id::new("menu_id"));
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -50,7 +39,7 @@ pub enum Action {
Hi3, Hi3,
} }
impl MenuAction for Action { impl widget::menu::Action for Action {
type Message = Message; type Message = Message;
fn message(&self) -> Message { fn message(&self) -> Message {
@ -129,7 +118,7 @@ impl cosmic::Application for App {
} }
/// Creates the application, and optionally emits task on initialize. /// Creates the application, and optionally emits task on initialize.
fn init(core: Core, input: Self::Flags) -> (Self, Task<Self::Message>) { fn init(core: Core, input: Self::Flags) -> (Self, cosmic::app::Task<Self::Message>) {
let mut nav_model = nav_bar::Model::default(); let mut nav_model = nav_bar::Model::default();
for (title, content) in input { for (title, content) in input {
@ -158,13 +147,13 @@ impl cosmic::Application for App {
} }
/// Called when a navigation item is selected. /// Called when a navigation item is selected.
fn on_nav_select(&mut self, id: nav_bar::Id) -> Task<Self::Message> { fn on_nav_select(&mut self, id: nav_bar::Id) -> cosmic::app::Task<Self::Message> {
self.nav_model.activate(id); self.nav_model.activate(id);
self.update_title() self.update_title()
} }
/// Handle application events here. /// Handle application events here.
fn update(&mut self, message: Self::Message) -> Task<Self::Message> { fn update(&mut self, message: Self::Message) -> cosmic::app::Task<Self::Message> {
match message { match message {
Message::Input1(v) => { Message::Input1(v) => {
self.input_1 = v; self.input_1 = v;
@ -195,46 +184,49 @@ impl cosmic::Application for App {
} }
/// Creates a view after each update. /// Creates a view after each update.
fn view(&self) -> Element<Self::Message> { fn view(&self) -> Element<'_, Self::Message> {
let page_content = self let page_content = self
.nav_model .nav_model
.active_data::<String>() .active_data::<String>()
.map_or("No page selected", String::as_str); .map_or("No page selected", String::as_str);
let text = cosmic::widget::text(page_content); let centered = widget::container(
widget::column()
let centered = cosmic::widget::container( .push(widget::text::body(page_content))
column![ .push(
text, widget::text_input::text_input("", &self.input_1)
cosmic::widget::text_input::text_input("", &self.input_1) .on_input(Message::Input1)
.on_input(Message::Input1) .on_clear(Message::Ignore),
.on_clear(Message::Ignore),
cosmic::widget::text_input::secure_input(
"",
&self.input_1,
Some(Message::ToggleHide),
self.hidden
) )
.on_input(Message::Input1), .push(
cosmic::widget::text_input::text_input("", &self.input_1).on_input(Message::Input1), widget::text_input::secure_input(
cosmic::widget::text_input::search_input("", &self.input_2) "",
.on_input(Message::Input2) &self.input_1,
.on_clear(Message::Ignore), Some(Message::ToggleHide),
] self.hidden,
.spacing(cosmic::theme::spacing().space_s) )
.width(iced::Length::Fill) .on_input(Message::Input1),
.height(iced::Length::Shrink) )
.align_x(iced::Alignment::Center), .push(widget::text_input::text_input("", &self.input_2).on_input(Message::Input2))
.push(
widget::text_input::search_input("", &self.input_2)
.on_input(Message::Input2)
.on_clear(Message::Ignore),
)
.spacing(cosmic::theme::spacing().space_s)
.width(Length::Fill)
.height(Length::Shrink)
.align_x(Alignment::Center),
) )
.width(iced::Length::Fill) .width(Length::Fill)
.height(iced::Length::Shrink) .height(Length::Shrink)
.align_x(iced::Alignment::Center) .align_x(Alignment::Center)
.align_y(iced::Alignment::Center); .align_y(Alignment::Center);
Element::from(centered) Element::from(centered)
} }
fn header_start(&self) -> Vec<Element<Self::Message>> { fn header_start(&self) -> Vec<Element<'_, Self::Message>> {
vec![cosmic::widget::responsive_menu_bar().into_element( vec![cosmic::widget::responsive_menu_bar().into_element(
self.core(), self.core(),
&self.keybinds, &self.keybinds,
@ -322,7 +314,7 @@ where
.unwrap_or("Unknown Page") .unwrap_or("Unknown Page")
} }
fn update_title(&mut self) -> Task<Message> { fn update_title(&mut self) -> cosmic::app::Task<Message> {
let header_title = self.active_page_title().to_owned(); let header_title = self.active_page_title().to_owned();
let window_title = format!("{header_title} — COSMIC AppDemo"); let window_title = format!("{header_title} — COSMIC AppDemo");
self.set_header_title(header_title); self.set_header_title(header_title);