-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose wrap width in TextLayout, add LineBreaking options
With this patch, the TextLayout object can now be given a width for wrapping words. This is exposed in the Label via a new LineBreaking enum, with three options. With LineBreaking::WordWrap, the TextLayout's wrap-width is set to the maximum width of the label's BoxConstraints, and lines are broken appropriately. The other two options (LineBreaking::Overflow and LineBreaking::Clip) both disable line-breaking; in the former case if text exceeds the width of the label it is painted outside of the label's bounds, and in the latter case the text is clipped to the bounds of the label. It would be nice if, in the clipping case, we could use a gradient mask or something to fade the edges of the clipped text, but I don't believe this is currently possible in piet. A simple alternative of drawing a gradient from (transparent -> label background color) doesn't work, because label's do not have an explicit background color, which I think is also correct. progress on #1192
- Loading branch information
Showing
6 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2020 The Druid Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! An example of various text layout features. | ||
use druid::widget::{Controller, Flex, Label, LineBreaking, RadioGroup, Scroll}; | ||
use druid::{ | ||
AppLauncher, Color, Data, Env, Lens, LocalizedString, UpdateCtx, Widget, WidgetExt, WindowDesc, | ||
}; | ||
|
||
const WINDOW_TITLE: LocalizedString<AppState> = LocalizedString::new("Text Options"); | ||
|
||
const 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. | ||
This 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."#; | ||
|
||
const SPACER_SIZE: f64 = 8.0; | ||
|
||
#[derive(Clone, Data, Lens)] | ||
struct AppState { | ||
/// the width at which to wrap lines. | ||
line_break_mode: LineBreaking, | ||
} | ||
|
||
/// A controller that sets properties on a label. | ||
struct LabelController; | ||
|
||
impl Controller<AppState, Label<AppState>> for LabelController { | ||
#[allow(clippy::float_cmp)] | ||
fn update( | ||
&mut self, | ||
child: &mut Label<AppState>, | ||
ctx: &mut UpdateCtx, | ||
old_data: &AppState, | ||
data: &AppState, | ||
env: &Env, | ||
) { | ||
if old_data.line_break_mode != data.line_break_mode { | ||
child.set_line_break_mode(data.line_break_mode); | ||
ctx.request_layout(); | ||
} | ||
child.update(ctx, old_data, data, env); | ||
} | ||
} | ||
|
||
pub fn main() { | ||
// describe the main window | ||
let main_window = WindowDesc::new(build_root_widget) | ||
.title(WINDOW_TITLE) | ||
.window_size((400.0, 600.0)); | ||
|
||
// create the initial app state | ||
let initial_state = AppState { | ||
line_break_mode: LineBreaking::Clip, | ||
}; | ||
|
||
// start the application | ||
AppLauncher::with_window(main_window) | ||
.use_simple_logger() | ||
.launch(initial_state) | ||
.expect("Failed to launch application"); | ||
} | ||
|
||
fn build_root_widget() -> impl Widget<AppState> { | ||
let label = Scroll::new( | ||
Label::new(TEXT) | ||
.with_text_color(Color::BLACK) | ||
.controller(LabelController) | ||
.background(Color::WHITE) | ||
.expand_width() | ||
.padding((SPACER_SIZE * 4.0, SPACER_SIZE)) | ||
.background(Color::grey8(222)), | ||
) | ||
.vertical(); | ||
|
||
let line_break_chooser = Flex::column() | ||
.with_child(Label::new("Line break mode")) | ||
.with_spacer(SPACER_SIZE) | ||
.with_child(RadioGroup::new(vec![ | ||
("Clip", LineBreaking::Clip), | ||
("Wrap", LineBreaking::WordWrap), | ||
("Overflow", LineBreaking::Overflow), | ||
])) | ||
.lens(AppState::line_break_mode); | ||
|
||
Flex::column() | ||
.with_spacer(SPACER_SIZE) | ||
.with_child(line_break_chooser) | ||
.with_spacer(SPACER_SIZE) | ||
.with_flex_child(label, 1.0) | ||
} |
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
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
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
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