Skip to content

Instantly share code, notes, and snippets.

@woodworker
Created October 6, 2018 12:51
Show Gist options
  • Save woodworker/b96453f05d15c50e7137912041a7c3b2 to your computer and use it in GitHub Desktop.
Save woodworker/b96453f05d15c50e7137912041a7c3b2 to your computer and use it in GitHub Desktop.
relm test
[package]
name = "relm-gtk-test"
version = "0.1.0"
authors = ["Martin Holzhauer <martin@holzhauer.eu>"]
[dependencies]
gtk = "^0.5.0"
relm = "^0.15.0"
relm-derive = "^0.15.0"
relm-attributes = "^0.15.0"
# gtk-test = "^0.2"
[features]
default = ["gtk_3_10"]
gtk_3_10 = ["gtk/v3_10"]
/*
* Copyright (c) 2018 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.
*/
extern crate gtk;
#[macro_use]
extern crate relm;
extern crate relm_attributes;
#[macro_use]
extern crate relm_derive;
// #[macro_use]
// extern crate gtk_test;
use gtk::{
ButtonExt,
Inhibit,
LabelExt,
OrientableExt,
WidgetExt,
GtkWindowExt,
HeaderBarExt,
};
use gtk::prelude::*;
use gtk::Orientation::{Vertical,Horizontal};
use relm::{Relm, Widget, timeout};
use relm_attributes::widget;
use self::Msg::*;
// Define the structure of the model.
pub struct Model {
counter: i32,
}
// The messages that can be sent to the update function.
#[derive(Msg)]
pub enum Msg {
Decrement,
Increment,
Quit,
Show,
}
#[widget]
impl Widget for Win {
fn init_view(&mut self) {
self.window.set_titlebar(&self.titlebar);
self.window.set_default_size(800, 600);
}
// The initial model.
fn model(relm: &Relm<Self>, _: ()) -> Model {
timeout(relm.stream(), 1000, || Show);
Model {
counter: 0,
}
}
// Update the model according to the message received.
fn update(&mut self, event: Msg) {
match event {
Decrement => self.model.counter -= 1,
Increment => self.model.counter += 1,
Quit => gtk::main_quit(),
Show => self.dec_button.set_visible(true),
}
}
view! {
#[name="window"]
gtk::Window {
title: "Meine Buttons",
gtk::Box {
// Set the orientation property of the Box.
orientation: Vertical,
// Create a Button inside the Box.
gtk::Button {
// Send the message Increment when the button is clicked.
clicked => Increment,
// TODO: check if using two events of the same name work.
label: "+",
},
#[name="label"]
gtk::Label {
// Bind the text property of the label to the counter attribute of the model.
text: &self.model.counter.to_string(),
},
#[name="dec_button"]
gtk::Button {
clicked => Decrement,
label: "-",
visible: false,
},
},
#[name="titlebar"]
gtk::HeaderBar {
show_close_button: true,
title: "Novelist",
subtitle: "<No Project>",
gtk::ToolButton {
icon_name: "document-new",
label: "Add",
},
gtk::ToolButton {
icon_name: "edit-delete",
label: "Delete",
},
gtk::ToolButton {
icon_name: "format-text-italic",
label: "Formatting",
},
gtk::ToolButton {
icon_name: "format-justify-left",
label: "Text Aligment",
},
gtk::ToolButton {
icon_name: "insert-text",
label: "Write Mode",
},
gtk::ToolButton {
icon_name: "preferences-system",
label: "Global Menu",
},
gtk::ToolButton {
icon_name: "document-save-as",
label: "Save As",
},
gtk::ToolButton {
icon_name: "document-save",
label: "Save",
},
gtk::SearchEntry {
child: {
pack_type: ::gtk::PackType::End,
},
// search_changed(entry) => Msg::Search(entry.get_text().unwrap().to_string()),
},
},
delete_event(_, _) => (Quit, Inhibit(false)),
}
}
}
fn main() {
Win::run(()).expect("Win::run failed");
}
// #[cfg(test)]
// mod tests {
// use gtk::LabelExt;
// use relm;
// use gtk_test::click;
// use Win;
// #[test]
// fn label_change() {
// let (_component, widgets) = relm::init_test::<Win>(()).expect("init_test failed");
// let dec_button = &widgets.dec_button;
// let label = &widgets.label;
// click(dec_button);
// assert_text!(label, -1);
// click(dec_button);
// assert_text!(label, -2);
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment