Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronErhardt committed May 20, 2024
1 parent f185644 commit 7841718
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 16 deletions.
48 changes: 43 additions & 5 deletions book/stable/component_macro/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,17 @@ <h3 id="naming-widgets"><a class="header" href="#naming-widgets">Naming widgets<
<p>Names can also be assigned with this syntax:</p>
<pre><code class="language-rust ignore">set_child: important_label = gtk::Label { ... }</code></pre>
<h3 id="conditional-widgets"><a class="header" href="#conditional-widgets">Conditional widgets</a></h3>
<p>The <code>view</code> macro allows you to include <code>if</code> and <code>match</code> statements for conditionally showing widgets.
Internally, the macro will use a <code>gtk::Stack</code>, so you can also use different <a href="https://gtk-rs.org/gtk4-rs/git/docs/gtk4/enum.StackTransitionType.html">transition types</a>.</p>
<p>The <code>view</code> macro allows you to include <code>if</code> and <code>match</code> statements for conditionally showing widgets.</p>
<p>Internally, the macro will use a <code>gtk::Stack</code>, so you can also use different <a href="https://gtk-rs.org/gtk4-rs/git/docs/gtk4/enum.StackTransitionType.html">transition types</a>.</p>
<pre><code class="language-rust ignore">if model.value % 2 == 0 {
gtk::Label {
set_label: "The value is even",
},
gtk::Label {
set_label: "The value is odd",
}
}

// Use a transition type to set an animation when the visible widget changes
}</code></pre>
<pre><code class="language-rust ignore">// Use a transition type to set an animation when the visible widget changes
#[transition = "SlideRight"]
match model.value {
0..=9 =&gt; {
Expand All @@ -273,6 +272,45 @@ <h3 id="conditional-widgets"><a class="header" href="#conditional-widgets">Condi
},
}
}</code></pre>
<p>If your conditional widget uses a <code>match</code> statement over an enum, you can destructure the enum in the <code>match</code> arms to access its variables with the help of the <code>#[track]</code> or <code>#[watch]</code> macros:</p>
<pre><code class="language-rust ignore">enum Foo {
Bar(f32),
Baz(String),
}

struct FooView {
foo: Foo
}

impl Component for FooView {
type Init = Foo;

// snip

view! {
#[root]
gtk::Box {
append = match &amp;model.foo {
Foo::Bar(num_value) =&gt; {
gtk::SpinButton {
// adding the `watch` macro lets you reference the destructured variables
#[watch]
set_value: num_value
}
}
Foo::Baz(str_value) =&gt; {
gtk::Label {
#[watch]
set_text: &amp;str_value
}
}
}
}
}

// snip
}</code></pre>
<p>Please note: if you attempt to destructure in the normal way - without the <code>track</code> or <code>watch</code> macros - you will get a compilation error, and Rust will 'fail to see' the destructured variables at the point where your code uses them. This is due to limitations in Relm4's component initialization strategy. Please ensure that you use one of those macros to avoid this.</p>
<h3 id="returned-widgets"><a class="header" href="#returned-widgets">Returned widgets</a></h3>
<p>Sometimes, methods used for assigning widgets return another widget.
For example, <a href="https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Stack.html#method.add_child"><code>gtk::Stack::add_child()</code></a> allows you to add a widget to the stack, but also returns a <code>gtk::StackPage</code> widget.
Expand Down
2 changes: 0 additions & 2 deletions book/stable/css/chrome.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* CSS for UI elements (a.k.a. chrome) */

@import 'variables.css';

html {
scrollbar-color: var(--scrollbar) var(--bg);
}
Expand Down
2 changes: 0 additions & 2 deletions book/stable/css/general.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* Base styles and content styles */

@import 'variables.css';

:root {
/* Browser default font-size is 16px, this way 1 rem = 10px */
font-size: 62.5%;
Expand Down
1 change: 1 addition & 0 deletions book/stable/highlight.js

Large diffs are not rendered by default.

48 changes: 43 additions & 5 deletions book/stable/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -4017,18 +4017,17 @@ <h3 id="naming-widgets"><a class="header" href="#naming-widgets">Naming widgets<
<p>Names can also be assigned with this syntax:</p>
<pre><code class="language-rust ignore">set_child: important_label = gtk::Label { ... }</code></pre>
<h3 id="conditional-widgets"><a class="header" href="#conditional-widgets">Conditional widgets</a></h3>
<p>The <code>view</code> macro allows you to include <code>if</code> and <code>match</code> statements for conditionally showing widgets.
Internally, the macro will use a <code>gtk::Stack</code>, so you can also use different <a href="https://gtk-rs.org/gtk4-rs/git/docs/gtk4/enum.StackTransitionType.html">transition types</a>.</p>
<p>The <code>view</code> macro allows you to include <code>if</code> and <code>match</code> statements for conditionally showing widgets.</p>
<p>Internally, the macro will use a <code>gtk::Stack</code>, so you can also use different <a href="https://gtk-rs.org/gtk4-rs/git/docs/gtk4/enum.StackTransitionType.html">transition types</a>.</p>
<pre><code class="language-rust ignore">if model.value % 2 == 0 {
gtk::Label {
set_label: "The value is even",
},
gtk::Label {
set_label: "The value is odd",
}
}

// Use a transition type to set an animation when the visible widget changes
}</code></pre>
<pre><code class="language-rust ignore">// Use a transition type to set an animation when the visible widget changes
#[transition = "SlideRight"]
match model.value {
0..=9 =&gt; {
Expand All @@ -4042,6 +4041,45 @@ <h3 id="conditional-widgets"><a class="header" href="#conditional-widgets">Condi
},
}
}</code></pre>
<p>If your conditional widget uses a <code>match</code> statement over an enum, you can destructure the enum in the <code>match</code> arms to access its variables with the help of the <code>#[track]</code> or <code>#[watch]</code> macros:</p>
<pre><code class="language-rust ignore">enum Foo {
Bar(f32),
Baz(String),
}

struct FooView {
foo: Foo
}

impl Component for FooView {
type Init = Foo;

// snip

view! {
#[root]
gtk::Box {
append = match &amp;model.foo {
Foo::Bar(num_value) =&gt; {
gtk::SpinButton {
// adding the `watch` macro lets you reference the destructured variables
#[watch]
set_value: num_value
}
}
Foo::Baz(str_value) =&gt; {
gtk::Label {
#[watch]
set_text: &amp;str_value
}
}
}
}
}

// snip
}</code></pre>
<p>Please note: if you attempt to destructure in the normal way - without the <code>track</code> or <code>watch</code> macros - you will get a compilation error, and Rust will 'fail to see' the destructured variables at the point where your code uses them. This is due to limitations in Relm4's component initialization strategy. Please ensure that you use one of those macros to avoid this.</p>
<h3 id="returned-widgets"><a class="header" href="#returned-widgets">Returned widgets</a></h3>
<p>Sometimes, methods used for assigning widgets return another widget.
For example, <a href="https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Stack.html#method.add_child"><code>gtk::Stack::add_child()</code></a> allows you to add a widget to the stack, but also returns a <code>gtk::StackPage</code> widget.
Expand Down
2 changes: 1 addition & 1 deletion book/stable/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion book/stable/searchindex.json

Large diffs are not rendered by default.

0 comments on commit 7841718

Please sign in to comment.