Skip to content

Commit aa133b1

Browse files
authored
Merge pull request #26 from compio-rs/external-parent
feat: child windows support
2 parents 624b9b5 + cc4ef6d commit aa133b1

35 files changed

Lines changed: 321 additions & 185 deletions

examples/basic.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
.with_max_level(compio_log::Level::INFO)
1616
.init();
1717

18-
App::new().run::<MainModel>(0, &());
18+
App::new().run::<MainModel>(0usize);
1919
}
2020

2121
struct MainModel {
@@ -36,13 +36,12 @@ enum MainMessage {
3636

3737
impl Component for MainModel {
3838
type Event = ();
39-
type Init = usize;
39+
type Init<'a> = usize;
4040
type Message = MainMessage;
41-
type Root = ();
4241

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

4746
window.set_text("Basic example");
4847
window.set_size(Size::new(800.0, 600.0));

examples/fs.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
.with_max_level(compio_log::Level::INFO)
1717
.init();
1818

19-
App::new().run::<MainModel>("Cargo.toml", &())
19+
App::new().run::<MainModel>("Cargo.toml")
2020
}
2121

2222
struct MainModel {
@@ -46,23 +46,23 @@ enum MainMessage {
4646

4747
impl Component for MainModel {
4848
type Event = ();
49-
type Init = &'static str;
49+
type Init<'a> = &'a str;
5050
type Message = MainMessage;
51-
type Root = ();
5251

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

58-
let canvas = Child::<Canvas>::init((), &window);
59-
let mut button = Child::<Button>::init((), &window);
57+
let canvas = Child::<Canvas>::init(&window);
58+
let mut button = Child::<Button>::init(&window);
6059
button.set_text("Choose file...");
6160

62-
let mut label = Child::<Label>::init((), &window);
61+
let mut label = Child::<Label>::init(&window);
6362
label.set_text(path);
6463
label.set_halign(HAlign::Center);
6564

65+
let path = path.to_string();
6666
spawn(fetch(path, sender.clone())).detach();
6767

6868
window.show();

examples/gallery.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
.with_max_level(compio_log::Level::INFO)
1515
.init();
1616

17-
App::new().run::<MainModel>(dirs::picture_dir(), &())
17+
App::new().run::<MainModel>(dirs::picture_dir())
1818
}
1919

2020
struct MainModel {
@@ -39,20 +39,19 @@ enum MainMessage {
3939

4040
impl Component for MainModel {
4141
type Event = ();
42-
type Init = Option<PathBuf>;
42+
type Init<'a> = Option<PathBuf>;
4343
type Message = MainMessage;
44-
type Root = ();
4544

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

51-
let canvas = Child::<Canvas>::init((), &window);
52-
let mut button = Child::<Button>::init((), &window);
50+
let canvas = Child::<Canvas>::init(&window);
51+
let mut button = Child::<Button>::init(&window);
5352
button.set_text("...");
5453

55-
let mut entry = Child::<Edit>::init((), &window);
54+
let mut entry = Child::<Edit>::init(&window);
5655
entry.set_text(
5756
path.as_ref()
5857
.map(|p| p.to_string_lossy().into_owned())

examples/mdi.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use winio::{
2+
App, Child, Component, ComponentSender, Layoutable, Size, Visible, Window, WindowEvent,
3+
};
4+
5+
fn main() {
6+
#[cfg(feature = "enable_log")]
7+
tracing_subscriber::fmt()
8+
.with_max_level(compio_log::Level::INFO)
9+
.init();
10+
11+
App::new().run::<MainModel>(());
12+
}
13+
14+
struct MainModel {
15+
window: Child<Window>,
16+
cwindow: Child<Window>,
17+
}
18+
19+
#[derive(Debug)]
20+
enum MainMessage {
21+
Noop,
22+
Close,
23+
Redraw,
24+
}
25+
26+
impl Component for MainModel {
27+
type Event = ();
28+
type Init<'a> = ();
29+
type Message = MainMessage;
30+
31+
fn init(_init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
32+
let mut window = Child::<Window>::init(());
33+
let mut cwindow = Child::<Window>::init(&window);
34+
35+
window.set_text("MDI example");
36+
window.set_size(Size::new(800.0, 600.0));
37+
38+
cwindow.set_text("Child window");
39+
cwindow.set_size(Size::new(400.0, 300.0));
40+
41+
cwindow.show();
42+
window.show();
43+
44+
Self { window, cwindow }
45+
}
46+
47+
async fn start(&mut self, sender: &ComponentSender<Self>) {
48+
let fut_window = self.window.start(
49+
sender,
50+
|e| match e {
51+
WindowEvent::Close => Some(MainMessage::Close),
52+
WindowEvent::Resize => Some(MainMessage::Redraw),
53+
_ => None,
54+
},
55+
|| MainMessage::Noop,
56+
);
57+
let fut_cwindow = self.cwindow.start(
58+
sender,
59+
|e| match e {
60+
WindowEvent::Resize => Some(MainMessage::Redraw),
61+
_ => None,
62+
},
63+
|| MainMessage::Noop,
64+
);
65+
futures_util::future::join(fut_window, fut_cwindow).await;
66+
}
67+
68+
async fn update(&mut self, message: Self::Message, sender: &ComponentSender<Self>) -> bool {
69+
futures_util::future::join(self.window.update(), self.cwindow.update()).await;
70+
match message {
71+
MainMessage::Noop => false,
72+
MainMessage::Close => {
73+
sender.output(());
74+
false
75+
}
76+
MainMessage::Redraw => true,
77+
}
78+
}
79+
80+
fn render(&mut self, _sender: &ComponentSender<Self>) {
81+
self.window.render();
82+
self.cwindow.render();
83+
}
84+
}

examples/net.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
.with_max_level(compio_log::Level::INFO)
1515
.init();
1616

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

2020
struct MainModel {
@@ -45,24 +45,24 @@ enum MainMessage {
4545

4646
impl Component for MainModel {
4747
type Event = ();
48-
type Init = &'static str;
48+
type Init<'a> = &'a str;
4949
type Message = MainMessage;
50-
type Root = ();
5150

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

57-
let canvas = Child::<Canvas>::init((), &window);
58-
let mut button = Child::<Button>::init((), &window);
56+
let canvas = Child::<Canvas>::init(&window);
57+
let mut button = Child::<Button>::init(&window);
5958
button.set_text("Go");
60-
let mut entry = Child::<Edit>::init((), &window);
59+
let mut entry = Child::<Edit>::init(&window);
6160
entry.set_text(url);
6261

6362
let client = Client::new();
6463

65-
spawn(fetch(client.clone(), url.to_string(), sender.clone())).detach();
64+
let url = url.to_string();
65+
spawn(fetch(client.clone(), url, sender.clone())).detach();
6666

6767
window.show();
6868

examples/widgets.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
.with_max_level(compio_log::Level::INFO)
1414
.init();
1515

16-
App::new().run::<MainModel>((), &());
16+
App::new().run::<MainModel>(());
1717
}
1818

1919
struct MainModel {
@@ -54,62 +54,61 @@ enum MainMessage {
5454

5555
impl Component for MainModel {
5656
type Event = ();
57-
type Init = ();
57+
type Init<'a> = ();
5858
type Message = MainMessage;
59-
type Root = ();
6059

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

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

68-
let mut ulabel = Child::<Label>::init((), &window);
67+
let mut ulabel = Child::<Label>::init(&window);
6968
ulabel.set_text("Username:");
7069
ulabel.set_halign(HAlign::Right);
71-
let mut plabel = Child::<Label>::init((), &window);
70+
let mut plabel = Child::<Label>::init(&window);
7271
plabel.set_text("Password:");
7372
plabel.set_halign(HAlign::Right);
7473

75-
let mut uentry = Child::<Edit>::init((), &window);
74+
let mut uentry = Child::<Edit>::init(&window);
7675
uentry.set_text("AAA");
77-
let mut pentry = Child::<Edit>::init((), &window);
76+
let mut pentry = Child::<Edit>::init(&window);
7877
pentry.set_password(true);
7978
pentry.set_text("123456");
8079

81-
let mut pcheck = Child::<CheckBox>::init((), &window);
80+
let mut pcheck = Child::<CheckBox>::init(&window);
8281
pcheck.set_checked(false);
8382
pcheck.set_text("Show");
8483

85-
let combo = Child::<ComboBox>::init((), &window);
84+
let combo = Child::<ComboBox>::init(&window);
8685

87-
let mut list = Child::<ObservableVec<String>>::init(vec![], &());
86+
let mut list = Child::<ObservableVec<String>>::init(vec![]);
8887
// https://www.zhihu.com/question/23600507/answer/140640887
8988
list.push("烫烫烫".into());
9089
list.push("昍昍昍".into());
9190
list.push("フフフフフフ".into());
9291
list.push("쳌쳌쳌".into());
9392

94-
let mut r1 = Child::<RadioButton>::init((), &window);
93+
let mut r1 = Child::<RadioButton>::init(&window);
9594
r1.set_text("屯屯屯");
9695
r1.set_checked(true);
97-
let mut r2 = Child::<RadioButton>::init((), &window);
96+
let mut r2 = Child::<RadioButton>::init(&window);
9897
r2.set_text("锟斤拷");
99-
let mut r3 = Child::<RadioButton>::init((), &window);
98+
let mut r3 = Child::<RadioButton>::init(&window);
10099
r3.set_text("╠╠╠");
101100

102-
let mut push_button = Child::<Button>::init((), &window);
101+
let mut push_button = Child::<Button>::init(&window);
103102
push_button.set_text("Push");
104-
let mut pop_button = Child::<Button>::init((), &window);
103+
let mut pop_button = Child::<Button>::init(&window);
105104
pop_button.set_text("Pop");
106-
let mut show_button = Child::<Button>::init((), &window);
105+
let mut show_button = Child::<Button>::init(&window);
107106
show_button.set_text("Show");
108107

109-
let mut progress = Child::<Progress>::init((), &window);
108+
let mut progress = Child::<Progress>::init(&window);
110109
progress.set_indeterminate(true);
111110

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

115114
window.show();

src/elm/button.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Component, ComponentSender, Layoutable, Point, Size, Visible, Window, ui};
1+
use crate::{BorrowedWindow, Component, ComponentSender, Layoutable, Point, Size, Visible, ui};
22

33
/// A simple button.
44
#[derive(Debug)]
@@ -59,12 +59,11 @@ pub enum ButtonEvent {
5959

6060
impl Component for Button {
6161
type Event = ButtonEvent;
62-
type Init = ();
62+
type Init<'a> = BorrowedWindow<'a>;
6363
type Message = ();
64-
type Root = Window;
6564

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

src/elm/canvas.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
Component, ComponentSender, DrawingContext, Layoutable, MouseButton, Point, Size, Visible,
3-
Window, ui,
2+
BorrowedWindow, Component, ComponentSender, DrawingContext, Layoutable, MouseButton, Point,
3+
Size, Visible, ui,
44
};
55

66
/// A simple drawing canvas.
@@ -59,12 +59,11 @@ pub enum CanvasEvent {
5959

6060
impl Component for Canvas {
6161
type Event = CanvasEvent;
62-
type Init = ();
62+
type Init<'a> = BorrowedWindow<'a>;
6363
type Message = ();
64-
type Root = Window;
6564

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

src/elm/check_box.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Component, ComponentSender, Layoutable, Point, Size, Visible, Window, ui};
1+
use crate::{BorrowedWindow, Component, ComponentSender, Layoutable, Point, Size, Visible, ui};
22

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

7070
impl Component for CheckBox {
7171
type Event = CheckBoxEvent;
72-
type Init = ();
72+
type Init<'a> = BorrowedWindow<'a>;
7373
type Message = ();
74-
type Root = Window;
7574

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

0 commit comments

Comments
 (0)