1#![windows_subsystem = "windows"]
19
20use druid::piet::{PietTextLayoutBuilder, TextStorage as PietTextStorage};
21use druid::text::{Attribute, RichText, TextStorage};
22use druid::widget::prelude::*;
23use druid::widget::{Controller, Flex, Label, LineBreaking, RadioGroup, RawLabel, Scroll};
24use druid::{
25 AppLauncher, Color, Data, FontFamily, FontStyle, FontWeight, Lens, LocalizedString,
26 TextAlignment, Widget, WidgetExt, WindowDesc,
27};
28
29const WINDOW_TITLE: LocalizedString<AppState> = LocalizedString::new("Text Options");
30
31const TEXT: &str = r#"Contrary to what we would like to believe, there is no such thing as a structureless group. Any group of people of whatever nature that comes together for any length of time for any purpose will inevitably structure itself in some fashion. The structure may be flexible; it may vary over time; it may evenly or unevenly distribute tasks, power and resources over the members of the group. But it will be formed regardless of the abilities, personalities, or intentions of the people involved. The very fact that we are individuals, with different talents, predispositions, and backgrounds makes this inevitable. Only if we refused to relate or interact on any basis whatsoever could we approximate structurelessness -- and that is not the nature of a human group.
32This means that to strive for a structureless group is as useful, and as deceptive, as to aim at an "objective" news story, "value-free" social science, or a "free" economy. A "laissez faire" group is about as realistic as a "laissez faire" society; the idea becomes a smokescreen for the strong or the lucky to establish unquestioned hegemony over others. This hegemony can be so easily established because the idea of "structurelessness" does not prevent the formation of informal structures, only formal ones. Similarly "laissez faire" philosophy did not prevent the economically powerful from establishing control over wages, prices, and distribution of goods; it only prevented the government from doing so. Thus structurelessness becomes a way of masking power, and within the women's movement is usually most strongly advocated by those who are the most powerful (whether they are conscious of their power or not). As long as the structure of the group is informal, the rules of how decisions are made are known only to a few and awareness of power is limited to those who know the rules. Those who do not know the rules and are not chosen for initiation must remain in confusion, or suffer from paranoid delusions that something is happening of which they are not quite aware."#;
33
34const SPACER_SIZE: f64 = 8.0;
35
36#[derive(Clone, Data, Lens)]
37struct AppState {
38 text: RichText,
39 line_break_mode: LineBreaking,
40 alignment: TextAlignment,
41}
42
43impl PietTextStorage for AppState {
47 fn as_str(&self) -> &str {
48 self.text.as_str()
49 }
50}
51
52impl TextStorage for AppState {
53 fn add_attributes(&self, builder: PietTextLayoutBuilder, env: &Env) -> PietTextLayoutBuilder {
54 self.text.add_attributes(builder, env)
55 }
56}
57
58struct LabelController;
60
61impl Controller<AppState, RawLabel<AppState>> for LabelController {
62 #[allow(clippy::float_cmp)]
63 fn update(
64 &mut self,
65 child: &mut RawLabel<AppState>,
66 ctx: &mut UpdateCtx,
67 old_data: &AppState,
68 data: &AppState,
69 env: &Env,
70 ) {
71 if old_data.line_break_mode != data.line_break_mode {
72 child.set_line_break_mode(data.line_break_mode);
73 ctx.request_layout();
74 }
75 if old_data.alignment != data.alignment {
76 child.set_text_alignment(data.alignment);
77 ctx.request_layout();
78 }
79 child.update(ctx, old_data, data, env);
80 }
81}
82
83pub fn main() {
84 let main_window = WindowDesc::new(build_root_widget())
86 .title(WINDOW_TITLE)
87 .window_size((400.0, 600.0));
88
89 let text = RichText::new(TEXT.into())
90 .with_attribute(0..9, Attribute::text_color(Color::rgb(1.0, 0.2, 0.1)))
91 .with_attribute(0..9, Attribute::size(24.0))
92 .with_attribute(0..9, Attribute::font_family(FontFamily::SERIF))
93 .with_attribute(194..239, Attribute::weight(FontWeight::BOLD))
94 .with_attribute(764.., Attribute::size(12.0))
95 .with_attribute(764.., Attribute::style(FontStyle::Italic));
96
97 let initial_state = AppState {
99 line_break_mode: LineBreaking::Clip,
100 alignment: Default::default(),
101 text,
102 };
103
104 AppLauncher::with_window(main_window)
106 .log_to_console()
107 .launch(initial_state)
108 .expect("Failed to launch application");
109}
110
111fn build_root_widget() -> impl Widget<AppState> {
112 let label = Scroll::new(
113 RawLabel::new()
114 .with_text_color(Color::BLACK)
115 .controller(LabelController)
116 .background(Color::WHITE)
117 .expand_width()
118 .padding((SPACER_SIZE * 4.0, SPACER_SIZE))
119 .background(Color::grey8(222)),
120 )
121 .vertical();
122
123 let line_break_chooser = Flex::column()
124 .with_child(Label::new("Line break mode"))
125 .with_spacer(SPACER_SIZE)
126 .with_child(RadioGroup::column(vec![
127 ("Clip", LineBreaking::Clip),
128 ("Wrap", LineBreaking::WordWrap),
129 ("Overflow", LineBreaking::Overflow),
130 ]))
131 .lens(AppState::line_break_mode);
132
133 let alignment_picker = Flex::column()
134 .with_child(Label::new("Justification"))
135 .with_spacer(SPACER_SIZE)
136 .with_child(RadioGroup::column(vec![
137 ("Start", TextAlignment::Start),
138 ("End", TextAlignment::End),
139 ("Center", TextAlignment::Center),
140 ("Justified", TextAlignment::Justified),
141 ]))
142 .lens(AppState::alignment);
143
144 let controls = Flex::row()
145 .cross_axis_alignment(druid::widget::CrossAxisAlignment::Start)
146 .with_child(alignment_picker)
147 .with_spacer(SPACER_SIZE)
148 .with_child(line_break_chooser)
149 .padding(SPACER_SIZE);
150
151 Flex::column()
152 .cross_axis_alignment(druid::widget::CrossAxisAlignment::Start)
153 .with_child(controls)
154 .with_flex_child(label, 1.0)
155}