Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
.with_max_level(compio_log::Level::INFO)
.init();

App::new().run::<MainModel>(0, &());
App::new().run::<MainModel>(0usize);
}

struct MainModel {
Expand All @@ -36,13 +36,12 @@ enum MainMessage {

impl Component for MainModel {
type Event = ();
type Init = usize;
type Init<'a> = usize;
type Message = MainMessage;
type Root = ();

fn init(counter: Self::Init, _root: &Self::Root, sender: &ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init((), &());
let canvas = Child::<Canvas>::init((), &window);
fn init(counter: Self::Init<'_>, sender: &ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init(());
let canvas = Child::<Canvas>::init(&window);

window.set_text("Basic example");
window.set_size(Size::new(800.0, 600.0));
Expand Down
16 changes: 8 additions & 8 deletions examples/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
.with_max_level(compio_log::Level::INFO)
.init();

App::new().run::<MainModel>("Cargo.toml", &())
App::new().run::<MainModel>("Cargo.toml")
}

struct MainModel {
Expand Down Expand Up @@ -46,23 +46,23 @@ enum MainMessage {

impl Component for MainModel {
type Event = ();
type Init = &'static str;
type Init<'a> = &'a str;
type Message = MainMessage;
type Root = ();

fn init(path: Self::Init, _root: &Self::Root, sender: &winio::ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init((), &());
fn init(path: Self::Init<'_>, sender: &winio::ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init(());
window.set_text("File IO example");
window.set_size(Size::new(800.0, 600.0));

let canvas = Child::<Canvas>::init((), &window);
let mut button = Child::<Button>::init((), &window);
let canvas = Child::<Canvas>::init(&window);
let mut button = Child::<Button>::init(&window);
button.set_text("Choose file...");

let mut label = Child::<Label>::init((), &window);
let mut label = Child::<Label>::init(&window);
label.set_text(path);
label.set_halign(HAlign::Center);

let path = path.to_string();
spawn(fetch(path, sender.clone())).detach();

window.show();
Expand Down
15 changes: 7 additions & 8 deletions examples/gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
.with_max_level(compio_log::Level::INFO)
.init();

App::new().run::<MainModel>(dirs::picture_dir(), &())
App::new().run::<MainModel>(dirs::picture_dir())
}

struct MainModel {
Expand All @@ -39,20 +39,19 @@ enum MainMessage {

impl Component for MainModel {
type Event = ();
type Init = Option<PathBuf>;
type Init<'a> = Option<PathBuf>;
type Message = MainMessage;
type Root = ();

fn init(path: Self::Init, _root: &Self::Root, sender: &winio::ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init((), &());
fn init(path: Self::Init<'_>, sender: &winio::ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init(());
window.set_text("Gallery example");
window.set_size(Size::new(800.0, 600.0));

let canvas = Child::<Canvas>::init((), &window);
let mut button = Child::<Button>::init((), &window);
let canvas = Child::<Canvas>::init(&window);
let mut button = Child::<Button>::init(&window);
button.set_text("...");

let mut entry = Child::<Edit>::init((), &window);
let mut entry = Child::<Edit>::init(&window);
entry.set_text(
path.as_ref()
.map(|p| p.to_string_lossy().into_owned())
Expand Down
84 changes: 84 additions & 0 deletions examples/mdi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use winio::{
App, Child, Component, ComponentSender, Layoutable, Size, Visible, Window, WindowEvent,
};

fn main() {
#[cfg(feature = "enable_log")]
tracing_subscriber::fmt()
.with_max_level(compio_log::Level::INFO)
.init();

App::new().run::<MainModel>(());
}

struct MainModel {
window: Child<Window>,
cwindow: Child<Window>,
}

#[derive(Debug)]
enum MainMessage {
Noop,
Close,
Redraw,
}

impl Component for MainModel {
type Event = ();
type Init<'a> = ();
type Message = MainMessage;

fn init(_init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init(());
let mut cwindow = Child::<Window>::init(&window);

window.set_text("MDI example");
window.set_size(Size::new(800.0, 600.0));

cwindow.set_text("Child window");
cwindow.set_size(Size::new(400.0, 300.0));

cwindow.show();
window.show();

Self { window, cwindow }
}

async fn start(&mut self, sender: &ComponentSender<Self>) {
let fut_window = self.window.start(
sender,
|e| match e {
WindowEvent::Close => Some(MainMessage::Close),
WindowEvent::Resize => Some(MainMessage::Redraw),
_ => None,
},
|| MainMessage::Noop,
);
let fut_cwindow = self.cwindow.start(
sender,
|e| match e {
WindowEvent::Resize => Some(MainMessage::Redraw),
_ => None,
},
|| MainMessage::Noop,
);
futures_util::future::join(fut_window, fut_cwindow).await;
}

async fn update(&mut self, message: Self::Message, sender: &ComponentSender<Self>) -> bool {
futures_util::future::join(self.window.update(), self.cwindow.update()).await;
match message {
MainMessage::Noop => false,
MainMessage::Close => {
sender.output(());
false
}
MainMessage::Redraw => true,
}
}

fn render(&mut self, _sender: &ComponentSender<Self>) {
self.window.render();
self.cwindow.render();
}
}
18 changes: 9 additions & 9 deletions examples/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
.with_max_level(compio_log::Level::INFO)
.init();

App::new().run::<MainModel>("https://www.example.com/", &());
App::new().run::<MainModel>("https://www.example.com/");
}

struct MainModel {
Expand Down Expand Up @@ -45,24 +45,24 @@ enum MainMessage {

impl Component for MainModel {
type Event = ();
type Init = &'static str;
type Init<'a> = &'a str;
type Message = MainMessage;
type Root = ();

fn init(url: Self::Init, _root: &Self::Root, sender: &winio::ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init((), &());
fn init(url: Self::Init<'_>, sender: &winio::ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init(());
window.set_text("Networking example");
window.set_size(Size::new(800.0, 600.0));

let canvas = Child::<Canvas>::init((), &window);
let mut button = Child::<Button>::init((), &window);
let canvas = Child::<Canvas>::init(&window);
let mut button = Child::<Button>::init(&window);
button.set_text("Go");
let mut entry = Child::<Edit>::init((), &window);
let mut entry = Child::<Edit>::init(&window);
entry.set_text(url);

let client = Client::new();

spawn(fetch(client.clone(), url.to_string(), sender.clone())).detach();
let url = url.to_string();
spawn(fetch(client.clone(), url, sender.clone())).detach();

window.show();

Expand Down
41 changes: 20 additions & 21 deletions examples/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
.with_max_level(compio_log::Level::INFO)
.init();

App::new().run::<MainModel>((), &());
App::new().run::<MainModel>(());
}

struct MainModel {
Expand Down Expand Up @@ -54,62 +54,61 @@ enum MainMessage {

impl Component for MainModel {
type Event = ();
type Init = ();
type Init<'a> = ();
type Message = MainMessage;
type Root = ();

fn init(_init: Self::Init, _root: &Self::Root, _sender: &ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init((), &());
let canvas = Child::<Canvas>::init((), &window);
fn init(_init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let mut window = Child::<Window>::init(());
let canvas = Child::<Canvas>::init(&window);

window.set_text("Widgets example");
window.set_size(Size::new(800.0, 600.0));

let mut ulabel = Child::<Label>::init((), &window);
let mut ulabel = Child::<Label>::init(&window);
ulabel.set_text("Username:");
ulabel.set_halign(HAlign::Right);
let mut plabel = Child::<Label>::init((), &window);
let mut plabel = Child::<Label>::init(&window);
plabel.set_text("Password:");
plabel.set_halign(HAlign::Right);

let mut uentry = Child::<Edit>::init((), &window);
let mut uentry = Child::<Edit>::init(&window);
uentry.set_text("AAA");
let mut pentry = Child::<Edit>::init((), &window);
let mut pentry = Child::<Edit>::init(&window);
pentry.set_password(true);
pentry.set_text("123456");

let mut pcheck = Child::<CheckBox>::init((), &window);
let mut pcheck = Child::<CheckBox>::init(&window);
pcheck.set_checked(false);
pcheck.set_text("Show");

let combo = Child::<ComboBox>::init((), &window);
let combo = Child::<ComboBox>::init(&window);

let mut list = Child::<ObservableVec<String>>::init(vec![], &());
let mut list = Child::<ObservableVec<String>>::init(vec![]);
// https://www.zhihu.com/question/23600507/answer/140640887
list.push("烫烫烫".into());
list.push("昍昍昍".into());
list.push("フフフフフフ".into());
list.push("쳌쳌쳌".into());

let mut r1 = Child::<RadioButton>::init((), &window);
let mut r1 = Child::<RadioButton>::init(&window);
r1.set_text("屯屯屯");
r1.set_checked(true);
let mut r2 = Child::<RadioButton>::init((), &window);
let mut r2 = Child::<RadioButton>::init(&window);
r2.set_text("锟斤拷");
let mut r3 = Child::<RadioButton>::init((), &window);
let mut r3 = Child::<RadioButton>::init(&window);
r3.set_text("╠╠╠");

let mut push_button = Child::<Button>::init((), &window);
let mut push_button = Child::<Button>::init(&window);
push_button.set_text("Push");
let mut pop_button = Child::<Button>::init((), &window);
let mut pop_button = Child::<Button>::init(&window);
pop_button.set_text("Pop");
let mut show_button = Child::<Button>::init((), &window);
let mut show_button = Child::<Button>::init(&window);
show_button.set_text("Show");

let mut progress = Child::<Progress>::init((), &window);
let mut progress = Child::<Progress>::init(&window);
progress.set_indeterminate(true);

let mut mltext = Child::<TextBox>::init((), &window);
let mut mltext = Child::<TextBox>::init(&window);
mltext.set_text("This is an example of\nmulti-line text box.");

window.show();
Expand Down
9 changes: 4 additions & 5 deletions src/elm/button.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Component, ComponentSender, Layoutable, Point, Size, Visible, Window, ui};
use crate::{BorrowedWindow, Component, ComponentSender, Layoutable, Point, Size, Visible, ui};

/// A simple button.
#[derive(Debug)]
Expand Down Expand Up @@ -59,12 +59,11 @@ pub enum ButtonEvent {

impl Component for Button {
type Event = ButtonEvent;
type Init = ();
type Init<'a> = BorrowedWindow<'a>;
type Message = ();
type Root = Window;

fn init(_init: Self::Init, root: &Self::Root, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::Button::new(root);
fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::Button::new(init);
Self { widget }
}

Expand Down
11 changes: 5 additions & 6 deletions src/elm/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
Component, ComponentSender, DrawingContext, Layoutable, MouseButton, Point, Size, Visible,
Window, ui,
BorrowedWindow, Component, ComponentSender, DrawingContext, Layoutable, MouseButton, Point,
Size, Visible, ui,
};

/// A simple drawing canvas.
Expand Down Expand Up @@ -59,12 +59,11 @@ pub enum CanvasEvent {

impl Component for Canvas {
type Event = CanvasEvent;
type Init = ();
type Init<'a> = BorrowedWindow<'a>;
type Message = ();
type Root = Window;

fn init(_init: Self::Init, root: &Self::Root, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::Canvas::new(root);
fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::Canvas::new(init);
Self { widget }
}

Expand Down
9 changes: 4 additions & 5 deletions src/elm/check_box.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Component, ComponentSender, Layoutable, Point, Size, Visible, Window, ui};
use crate::{BorrowedWindow, Component, ComponentSender, Layoutable, Point, Size, Visible, ui};

/// A simple check box.
#[derive(Debug)]
Expand Down Expand Up @@ -69,12 +69,11 @@ pub enum CheckBoxEvent {

impl Component for CheckBox {
type Event = CheckBoxEvent;
type Init = ();
type Init<'a> = BorrowedWindow<'a>;
type Message = ();
type Root = Window;

fn init(_init: Self::Init, root: &Self::Root, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::CheckBox::new(root);
fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::CheckBox::new(init);
Self { widget }
}

Expand Down
Loading