Skip to content

Commit

Permalink
Add multi containers
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Apr 29, 2017
1 parent fa5290f commit c94c354
Show file tree
Hide file tree
Showing 9 changed files with 817 additions and 291 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ documentation = "https://docs.rs/relm/"
license = "MIT"
name = "relm"
repository = "https://github.com/antoyo/relm"
version = "0.9.0"
version = "0.9.1"

[badges]
travis-ci = { repository = "antoyo/relm" }
Expand Down
160 changes: 160 additions & 0 deletions examples/multi-container-attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright (c) 2017 Boucher, Antoni <bouanto@zoho.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#![feature(proc_macro)]

extern crate gtk;
#[macro_use]
extern crate relm;
extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;

use gtk::{
ButtonExt,
Inhibit,
OrientableExt,
WidgetExt,
};
use gtk::Orientation::{Horizontal, Vertical};
use relm::Widget;
use relm_attributes::widget;

use self::Msg::*;

#[widget]
impl Widget for CenterButton {
fn model() -> () {
}

fn update(&mut self, _msg: (), _model: &mut ()) {
}

view! {
#[data="center"]
gtk::Button {
label: "-",
},
}
}

#[widget]
impl Widget for Button {
fn model() -> () {
}

fn update(&mut self, _msg: (), _model: &mut ()) {
}

view! {
#[data="right"]
gtk::Button {
label: "+",
},
}
}

#[widget]
impl Widget for VBox {
fn model() -> () {
()
}

fn update(&mut self, _event: Msg, _model: &mut ()) {
}

view! {
gtk::Box {
orientation: Horizontal,
// Specify where the widgets will be added in this container by default.
#[container]
gtk::Box {
orientation: Vertical,
},
// Specify where the widgets will be added in this container when the child's data is
// "center".
#[container="center"]
gtk::Box {
orientation: Vertical,
},
#[container="right"]
gtk::Box {
orientation: Vertical,
}
}
}
}

#[derive(Clone)]
pub struct Model {
counter: i32,
}

#[derive(Msg)]
pub enum Msg {
Decrement,
Increment,
Quit,
}

#[widget]
impl Widget for Win {
fn model() -> Model {
Model {
counter: 0,
}
}

fn update(&mut self, event: Msg, model: &mut Model) {
match event {
Decrement => model.counter -= 1,
Increment => model.counter += 1,
Quit => gtk::main_quit(),
}
}

view! {
gtk::Window {
VBox {
gtk::Button {
clicked => Increment,
label: "+",
},
gtk::Label {
text: &model.counter.to_string(),
},
Button {
},
CenterButton {
},
gtk::Button {
clicked => Decrement,
label: "-",
},
},
delete_event(_, _) => (Quit, Inhibit(false)),
}
}
}

fn main() {
Win::run(()).unwrap();
}
228 changes: 228 additions & 0 deletions examples/multi-container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Copyright (c) 2017 Boucher, Antoni <bouanto@zoho.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#![feature(proc_macro)]

extern crate gtk;
#[macro_use]
extern crate relm;
extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;

use gtk::{
ContainerExt,
Inhibit,
Label,
WidgetExt,
Window,
};
use gtk::Orientation::{Horizontal, Vertical};
use gtk::WindowType::Toplevel;
use relm::{Component, Container, ContainerWidget, RelmContainer, RemoteRelm, Widget};

use self::Msg::*;

#[derive(Clone)]
struct CenterButton {
button: gtk::Button,
}

impl Widget for CenterButton {
type Model = ();
type ModelParam = ();
type Msg = ();
type Root = gtk::Button;

fn data() -> Option<&'static str> {
Some("center")
}

fn model(_: ()) -> () {
}

fn root(&self) -> &Self::Root {
&self.button
}

fn update(&mut self, _msg: (), _model: &mut ()) {
}

fn view(_relm: &RemoteRelm<Self>, _model: &()) -> Self {
let button = gtk::Button::new_with_label("-");
CenterButton {
button: button,
}
}
}

#[derive(Clone)]
struct Button {
button: gtk::Button,
}

impl Widget for Button {
type Model = ();
type ModelParam = ();
type Msg = ();
type Root = gtk::Button;

fn data() -> Option<&'static str> {
Some("right")
}

fn model(_: ()) -> () {
}

fn root(&self) -> &Self::Root {
&self.button
}

fn update(&mut self, _msg: (), _model: &mut ()) {
}

fn view(_relm: &RemoteRelm<Self>, _model: &()) -> Self {
let button = gtk::Button::new_with_label("+");
Button {
button: button,
}
}
}

#[derive(Clone)]
struct SplitBox {
hbox1: gtk::Box,
hbox2: gtk::Box,
hbox3: gtk::Box,
vbox: gtk::Box,
}

impl Container for SplitBox {
type Container = gtk::Box;

fn container(&self) -> &Self::Container {
&self.hbox1
}

fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) {
if WIDGET::data() == Some("right") {
self.hbox3.add(widget.root());
}
else if WIDGET::data() == Some("center") {
self.hbox2.add(widget.root());
}
else {
self.hbox1.add(widget.root());
}
}
}

impl Widget for SplitBox {
type Model = ();
type ModelParam = ();
type Msg = ();
type Root = gtk::Box;

fn model(_: ()) -> () {
()
}

fn root(&self) -> &Self::Root {
&self.vbox
}

fn update(&mut self, _event: (), _model: &mut ()) {
}

fn view(_relm: &RemoteRelm<Self>, _model: &Self::Model) -> Self {
let vbox = gtk::Box::new(Horizontal, 0);
let hbox1 = gtk::Box::new(Vertical, 0);
vbox.add(&hbox1);
let hbox2 = gtk::Box::new(Vertical, 0);
vbox.add(&hbox2);
let hbox3 = gtk::Box::new(Vertical, 0);
vbox.add(&hbox3);
SplitBox {
hbox1,
hbox2,
hbox3,
vbox,
}
}
}

#[derive(Msg)]
pub enum Msg {
Quit,
}

#[derive(Clone)]
struct Win {
button: Component<Button>,
center_button: Component<CenterButton>,
vbox: Component<SplitBox>,
window: Window,
}

impl Widget for Win {
type Model = ();
type ModelParam = ();
type Msg = Msg;
type Root = Window;

fn model(_: ()) -> () {
}

fn root(&self) -> &Self::Root {
&self.window
}

fn update(&mut self, event: Msg, _model: &mut ()) {
match event {
Quit => gtk::main_quit(),
}
}

fn view(relm: &RemoteRelm<Self>, _model: &()) -> Self {
let window = Window::new(Toplevel);
let vbox = window.add_widget::<SplitBox, _>(&relm, ());
let plus_button = gtk::Button::new_with_label("+");
vbox.add(&plus_button);
let label = Label::new(Some("0"));
vbox.add(&label);
let button = vbox.add_widget::<Button, _>(&relm, ());
let center_button = vbox.add_widget::<CenterButton, _>(&relm, ());
let minus_button = gtk::Button::new_with_label("-");
vbox.add(&minus_button);
connect!(relm, window, connect_delete_event(_, _) (Some(Quit), Inhibit(false)));
window.show_all();
Win {
button: button,
center_button,
vbox: vbox,
window: window,
}
}
}

fn main() {
Win::run(()).unwrap();
}
Loading

0 comments on commit c94c354

Please sign in to comment.