Skip to content

Commit

Permalink
Rename data to parent_id
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Apr 30, 2017
1 parent 6257c50 commit da92f6c
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -6,7 +6,7 @@ documentation = "https://docs.rs/relm/"
license = "MIT"
name = "relm"
repository = "https://github.com/antoyo/relm"
version = "0.9.2"
version = "0.9.3"

[badges]
travis-ci = { repository = "antoyo/relm" }
Expand Down
6 changes: 3 additions & 3 deletions examples/multi-container-attribute.rs
Expand Up @@ -65,7 +65,7 @@ impl Widget for CenterButton {
}

view! {
#[data="center"]
#[parent="center"]
gtk::Button {
label: "-",
},
Expand All @@ -81,7 +81,7 @@ impl Widget for Button {
}

view! {
#[data="right"]
#[parent="right"]
gtk::Button {
label: "+",
},
Expand All @@ -105,7 +105,7 @@ impl Widget for SplitBox {
gtk::Box {
orientation: Vertical,
},
// Specify where the widgets will be added in this container when the child's data is
// Specify where the widgets will be added in this container when the child's parent id is
// "center".
#[container="center"]
gtk::Frame {
Expand Down
16 changes: 8 additions & 8 deletions examples/multi-container.rs
Expand Up @@ -53,11 +53,11 @@ impl Widget for CenterButton {
type Msg = ();
type Root = gtk::Button;

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

fn model(_: ()) -> () {
fn parent_id() -> Option<&'static str> {
Some("center")
}

fn root(&self) -> &Self::Root {
Expand Down Expand Up @@ -86,11 +86,11 @@ impl Widget for Button {
type Msg = ();
type Root = gtk::Button;

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

fn model(_: ()) -> () {
fn parent_id() -> Option<&'static str> {
Some("right")
}

fn root(&self) -> &Self::Root {
Expand Down Expand Up @@ -161,11 +161,11 @@ impl Container for SplitBox {
}

fn add_widget<WIDGET: Widget>(&self, widget: &WIDGET) -> gtk::Container {
if WIDGET::data() == Some("right") {
if WIDGET::parent_id() == Some("right") {
self.hbox3.add(widget.root());
self.hbox3.widget().root().clone().upcast()
}
else if WIDGET::data() == Some("center") {
else if WIDGET::parent_id() == Some("center") {
self.hbox2.add(widget.root());
self.hbox2.clone().upcast()
}
Expand Down
2 changes: 1 addition & 1 deletion relm-gen-widget/Cargo.toml
Expand Up @@ -5,7 +5,7 @@ documentation = "https://docs.rs/relm-gen-widget/"
license = "MIT"
name = "relm-gen-widget"
repository = "https://github.com/antoyo/relm"
version = "0.9.8"
version = "0.9.9"

[dependencies]
lazy_static = "^0.2.4"
Expand Down
8 changes: 4 additions & 4 deletions relm-gen-widget/src/gen.rs
Expand Up @@ -384,7 +384,7 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
if container_names.len() > 1 {
let mut default_container = Tokens::new();
let mut other_containers = Tokens::new();
for (data, &(ref name, ref typ)) in container_names {
for (parent_id, &(ref name, ref typ)) in container_names {
let first_type_part = &typ.segments.first().expect("first segment").ident;
let (container_trait, upcast_container) =
if first_type_part == "gtk" {
Expand All @@ -401,7 +401,7 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
::relm::Cast::upcast(self.#name.widget().root().clone())
})
};
if data.is_none() {
if parent_id.is_none() {
default_container = quote! {
#container_trait::add(&self.#name, widget.root());
#upcast_container
Expand All @@ -410,7 +410,7 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
else {
if other_containers.as_str().is_empty() {
other_containers = quote! {
if WIDGET::data() == Some(#data) {
if WIDGET::parent_id() == Some(#parent_id) {
#container_trait::add(&self.#name, widget.root());
#upcast_container
}
Expand All @@ -419,7 +419,7 @@ fn gen_add_widget_method(container_names: &HashMap<Option<String>, (Ident, Path)
else {
other_containers = quote! {
#other_containers
else if WIDGET::data() == Some(#data) {
else if WIDGET::parent_id() == Some(#parent_id) {
#container_trait::add(&self.#name, widget.root());
#upcast_container
}
Expand Down
14 changes: 7 additions & 7 deletions relm-gen-widget/src/lib.rs
Expand Up @@ -85,9 +85,9 @@ pub struct Driver {
root_widget_type: Option<Tokens>,
update_method: Option<ImplItem>,
view_macro: Option<Mac>,
widget_data: Option<String>,
widget_model_type: Option<Ty>,
widget_msg_type: Option<Ty>,
widget_parent_id: Option<String>,
widgets: HashMap<Ident, Tokens>, // Map widget ident to widget type.
}

Expand Down Expand Up @@ -115,9 +115,9 @@ impl Driver {
root_widget_type: None,
update_method: None,
view_macro: None,
widget_data: None,
widget_model_type: None,
widget_msg_type: None,
widget_parent_id: None,
widgets: HashMap::new(),
}
}
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Driver {
Macro(mac) => self.view_macro = Some(mac),
Method(sig, _) => {
match item.ident.to_string().as_ref() {
"data" => self.data_method = Some(i),
"parent_id" => self.data_method = Some(i),
"root" => self.root_method = Some(i),
"model" => {
self.widget_model_type = Some(get_return_type(sig));
Expand Down Expand Up @@ -235,10 +235,10 @@ impl Driver {

fn get_data_method(&mut self) -> Option<ImplItem> {
self.data_method.take().or_else(|| {
if let Some(ref data) = self.widget_data {
if let Some(ref parent_id) = self.widget_parent_id {
Some(block_to_impl_item(quote! {
fn data() -> Option<&'static str> {
Some(#data)
fn parent_id() -> Option<&'static str> {
Some(#parent_id)
}
}))
}
Expand Down Expand Up @@ -325,7 +325,7 @@ impl Driver {
if let Gtk(ref mut widget) = widget.widget {
widget.relm_name = Some(typ.clone());
}
self.widget_data = widget.data.clone();
self.widget_parent_id = widget.parent_id.clone();
let mut properties_model_map = HashMap::new();
get_properties_model_map(&widget, &mut properties_model_map);
self.add_widgets(&widget, &properties_model_map);
Expand Down
14 changes: 7 additions & 7 deletions relm-gen-widget/src/parser.rs
Expand Up @@ -83,9 +83,9 @@ pub struct Widget {
pub child_properties: HashMap<String, Tokens>,
pub children: Vec<Widget>,
pub container_type: Option<Option<String>>,
pub data: Option<String>,
pub init_parameters: Vec<Tokens>,
pub name: syn::Ident,
pub parent_id: Option<String>,
pub properties: HashMap<String, Tokens>,
pub typ: Path,
pub widget: EitherWidget,
Expand All @@ -100,9 +100,9 @@ impl Widget {
child_properties,
children,
container_type: None,
data: None,
init_parameters,
name: syn::Ident::new(name),
parent_id: None,
properties,
typ,
widget: Gtk(widget),
Expand All @@ -121,9 +121,9 @@ impl Widget {
child_properties,
children,
container_type: None,
data: None,
init_parameters,
name: syn::Ident::new(name),
parent_id: None,
properties,
typ,
widget: Relm(widget),
Expand Down Expand Up @@ -202,8 +202,8 @@ pub fn parse(tokens: &[TokenTree]) -> Widget {
else {
tokens.to_vec()
};
let (mut widget, _, data) = parse_child(&tokens);
widget.data = data;
let (mut widget, _, parent_id) = parse_child(&tokens);
widget.parent_id = parent_id;
widget
}

Expand Down Expand Up @@ -274,8 +274,8 @@ fn parse_child(mut tokens: &[TokenTree]) -> (Widget, &[TokenTree], Option<String
widget.name = syn::Ident::new(name);
}
widget.container_type = container_type;
let data = attributes.get("data").and_then(|opt_str| opt_str.map(str::to_string));
(widget, new_tokens, data)
let parent_id = attributes.get("parent").and_then(|opt_str| opt_str.map(str::to_string));
(widget, new_tokens, parent_id)
}

fn parse_ident(tokens: &[TokenTree]) -> (String, &[TokenTree]) {
Expand Down
16 changes: 8 additions & 8 deletions src/widget.rs
Expand Up @@ -38,14 +38,6 @@ pub trait Widget
/// The type of the root widget.
type Root;

/// Get some string data.
/// This is useful for custom Container implementation: when you implement the
/// [`Container::add_widget()`](trait.Container.html#tymethod.add_widget), you might want to
/// insert widgets elsewhere depending of this data.
fn data() -> Option<&'static str> {
None
}

/// Update the view after it is initially created.
/// This method is only useful when using the `#[widget]` attribute, because when not using it,
/// you can use the [`view()`](trait.Widget.html#tymethod.view) method instead.
Expand All @@ -59,6 +51,14 @@ pub trait Widget
fn on_add<W: IsA<gtk::Widget> + IsA<Object>>(&self, _parent: W) {
}

/// Get the parent ID.
/// This is useful for custom Container implementation: when you implement the
/// [`Container::add_widget()`](trait.Container.html#tymethod.add_widget), you might want to
/// insert widgets elsewhere depending of this id.
fn parent_id() -> Option<&'static str> {
None
}

// TODO: ajouter une méthode param() pour déterminer des paramètres qui seront pris en compte à
// l’ajout du widget.

Expand Down

0 comments on commit da92f6c

Please sign in to comment.