Last active
April 25, 2017 20:11
-
-
Save GuillaumeGomez/8da689d87aae36cb96a3cb4634f1a603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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, | |
EntryExt, | |
Inhibit, | |
OrientableExt, | |
WidgetExt, | |
WindowExt, | |
}; | |
use gtk::Orientation::*; | |
use relm::Widget; | |
use relm_attributes::widget; | |
use self::Msg::*; | |
#[derive(Clone)] | |
pub struct Model { | |
content: Wrapper, | |
} | |
#[derive(Msg)] | |
pub enum Msg { | |
Add(String), | |
Remove(usize), | |
Quit, | |
} | |
#[derive(Clone)] | |
struct Wrapper(gtk::TextBuffer); | |
unsafe impl Send for Wrapper {} | |
#[widget] | |
impl Widget for Win { | |
fn model() -> Model { | |
Model { | |
content: Wrapper(gtk::TextBuffer::new(None)), | |
} | |
} | |
fn update(&mut self, event: Msg, model: &mut Model) { | |
match event { | |
Add(text) => { | |
let buffer = model.content.0.clone(); | |
if let Some(mark) = buffer.clone().get_insert() { | |
let mut iter = buffer.get_iter_at_mark(&mark); | |
buffer.insert(&mut iter, &text); | |
} | |
} | |
Remove(count) => { | |
let buffer = model.content.0.clone(); | |
let start = buffer.get_start_iter(); | |
let end = buffer.get_end_iter(); | |
if let Some(content) = buffer.get_text(&start, &end, true) { | |
if content.len() >= count { | |
let limit = content.len() - count; | |
buffer.set_text(&content[..limit]); | |
} else { | |
buffer.set_text(""); | |
} | |
} | |
} | |
Quit => gtk::main_quit(), | |
} | |
} | |
view! { | |
gtk::Window { | |
property_default_width: 400, | |
property_default_height: 600, | |
gtk::Box { | |
orientation: Vertical, | |
gtk::TextView { | |
buffer: Some(&model.content.0), | |
editable: false, | |
vexpand: true, | |
}, | |
gtk::Box { | |
orientation: Horizontal, | |
gtk::Entry { | |
activate(entry) => { | |
let text = entry.get_text().unwrap(); | |
entry.set_text(""); | |
Msg::Add(text) | |
}, | |
placeholder_text: "Text to add", | |
hexpand: true, | |
}, | |
gtk::Button { | |
clicked => Msg::Remove(4), | |
label: "Remove some string...", | |
}, | |
hexpand: true, | |
}, | |
}, | |
title: "fooooo", | |
delete_event(_, _) => (Quit, Inhibit(false)), | |
} | |
} | |
} | |
fn main() { | |
relm::run::<Win>().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment