Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- **Fixes:** the ttk widgets of the modern look have room at their edges, as in the themes of Tk, so a script with little `-padx` of its own, such as the Login dialog of tkchat, is no longer crowded (#1070). The notebook insets its tabs and its client area, and its page; a button, a check button, a radio button, and a menu button keep a margin for the focus ring inside their window; a label and a label frame have padding. The sizes come from the new spacing scale of the theme (`--space-1` to `--space-5`). `ttk::style lookup` gives them, and `configure` of `-padding`, `-margin`, and `-tabmargins` changes them.
- **Tests:** `ztk --headless --fake-native ios|android` runs a script with the fake native views of the headless backend, so a test of the native UI of an app runs in CI with no Simulator or emulator (#918).
- **iOS, Android:** a `ListScreen` has `insertRows` and `deleteRows`: after a change of the data, the rows come and go with the animation of the system on iOS, and with fades on Android (#915).
- **iOS, Android:** `$screen add image -image PHOTO` shows a Tk photo in a native image view of a screen or a stack (`UIImageView`, `ImageView`), at the width of the column or at `-height`, with `-fit` `fit`, `fill`, or `stretch` (#919).
Expand Down
12 changes: 6 additions & 6 deletions docs/debug/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Then it lists each mapped widget whose box has the point of the click, from `ztk
count 0
event .banner <Button-1>
event .banner <ButtonRelease-1>
at 45,72: . Ztk 0 0 91 96
at 45,72: .add Button 20 57 51 31
at 45,72: .banner Frame 0 68 91 28
at 45,70: . Ztk 0 0 91 92
at 45,70: .add Button 20 57 51 27
at 45,70: .banner Frame 0 64 91 28
```

The press and the release went to `.banner`, so the `-command` of Add did not run. The tree gives the reason: the box of `.banner` covers the point 45,72, the center of Add. The script made `.banner` after `.add`, so `.banner` is above `.add` in the stacking order, and it gets the click.
The press and the release went to `.banner`, so the `-command` of Add did not run. The tree gives the reason: the box of `.banner` covers the point 45,70, the center of Add. The script made `.banner` after `.add`, so `.banner` is above `.add` in the stacking order, and it gets the click.

## The fix

Expand All @@ -57,8 +57,8 @@ event .add <FocusIn>
event .add <ButtonRelease-1>
command .add incr count
event .n <Configure>
at 45,72: . Ztk 0 0 91 96
at 45,72: .add Button 20 57 51 31
at 45,70: . Ztk 0 0 91 92
at 45,70: .add Button 20 57 51 27
```

## The commands
Expand Down
28 changes: 14 additions & 14 deletions docs/debug/visual-diff.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ set d [ztk::debug diff before -image]
`ztk --headless --scale 2 examples/debug/visual-diff.tcl out` prints these lines and writes the three pictures into the folder `out`. The script prints each item of `moved` on its own line:

```text
pixels 9142
box 64 0 162 186
size {440 372} {452 372}
moved . {0 0 220 186} {0 0 226 186}
moved .f {0 0 220 186} {0 0 226 186}
moved .f.e2 {64 73 144 29} {70 73 144 29}
moved .f.save {151 145 57 31} {157 145 57 31}
pixels 8566
box 64 0 162 176
size {440 352} {452 352}
moved . {0 0 220 176} {0 0 226 176}
moved .f {0 0 220 176} {0 0 226 176}
moved .f.e2 {64 71 144 27} {70 71 144 27}
moved .f.save {151 139 57 27} {157 139 57 27}
```

<!-- figure: debug-diff-before | The order form before the change | Before: the mark. -->
Expand Down Expand Up @@ -63,12 +63,12 @@ The answer has only the keys with a change. The answer `pixels 0` with no other
`ztk::debug mark name ?path?` keeps the image of the toplevel of `path` (default `.`) and the state of each widget in it: the class, the box, the map state, and the text of each option. `ztk::debug changes name` gives the same answer as `diff`, as lines for a person. This answer is from a run at `--scale 1`:

```text
size 220x186 -> 226x186
2396 pixels changed in 64,0 162x186
moved . 0,0 220x186 -> 0,0 226x186
moved .f 0,0 220x186 -> 0,0 226x186
moved .f.e2 64,73 144x29 -> 70,73 144x29
moved .f.save 151,145 57x31 -> 157,145 57x31
size 220x176 -> 226x176
2252 pixels changed in 64,0 162x176
moved . 0,0 220x176 -> 0,0 226x176
moved .f 0,0 220x176 -> 0,0 226x176
moved .f.e2 64,71 144x27 -> 70,71 144x27
moved .f.save 151,139 57x27 -> 157,139 57x27
```

To save the diff picture, decode the `image` key:
Expand Down Expand Up @@ -108,7 +108,7 @@ if {[dict get $d pixels] == 0 && ![dict exists $d size]} {
}
```

`ztk --headless examples/debug/golden.tcl` prints `ok: the window equals golden-order.png`. With the change of padding (the argument `bug`), it prints `FAIL: 4131 pixels changed in 9,14 202x162` and exits with 1. The argument `update` writes the golden picture again.
`ztk --headless examples/debug/golden.tcl` prints `ok: the window equals golden-order.png`. With the change of padding (the argument `bug`), it prints `FAIL: 4051 pixels changed in 9,14 202x152` and exits with 1. The argument `update` writes the golden picture again.

Choose the tolerance with care:

Expand Down
3 changes: 2 additions & 1 deletion docs/differences.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Each platform has a native backend of its own, with no toolkit library between z
### Styles and themes

- The classic widgets have the look of the theme, as the `ttk` widgets do. One stylesheet styles both kinds. In Tk, the theme changes only the `ttk` widgets.
- `ttk::style configure` and `map` change the look through CSS for `-foreground`, `-background`, `-fieldbackground`, `-bordercolor`, `-borderwidth`, `-relief`, `-padding`, `-font`, and `-indicatorcolor`. Other options are stored for `lookup`.
- `ttk::style configure` and `map` change the look through CSS for `-foreground`, `-background`, `-fieldbackground`, `-bordercolor`, `-borderwidth`, `-relief`, `-padding`, `-margin`, `-tabmargins`, `-font`, and `-indicatorcolor`. Other options are stored for `lookup`. In the modern look, `lookup` of `-padding`, `-margin`, and `-tabmargins` gives the space of the theme of ztk, which differs from the numbers of `wish` ([The space of the theme](styling.md#the-space-of-the-theme)).
- In the modern look, `cget` of `-padx`, `-pady`, and `-highlightthickness` of `button`, `label`, `entry`, `checkbutton`, `radiobutton`, and `labelframe` gives the space that the look draws, such as `-padx` 12 of a button, when the script did not set the option ([Styling](styling.md#the-space-of-the-theme)). The default column of `configure` keeps the default of the option, such as 0 for `-padx` of a button. In `wish` 8.6.16 it is 3m. `ttk::style lookup` of `Item -padding` and `Heading -padding` gives the space of the cells and the headings of a Treeview.
- `ttk::style layout` and `element create` store their data with no effect on the look, because CSS gives the look of each part ([ttk::style](styling.md#ttkstyle)).
- In `ttk::style map`, a pair with more states wins over an earlier pair with fewer states. Tk takes the earlier pair.
- The states `active` and `hover` are one flag.
Expand Down
9 changes: 5 additions & 4 deletions docs/reference/ttk-notebook.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ command ttk::notebook {
summary {The width of the area of the pages, in place of the width of the widest page.}
description {A value above 0 sets the width of the page area. 0 makes the area as wide as the widest page. The padding adds to the requested width.}
example {
ttk::notebook .tnbwi -width 200 -padding 5
winfo reqwidth .tnbwi
} 212
ttk::notebook .tnbwi -width 200
ttk::notebook .tnbwi3 -width 300
expr {[winfo reqwidth .tnbwi3] - [winfo reqwidth .tnbwi]}
} 100
error {ttk::notebook .tnbwi2 -width x} {bad screen distance "x"} {TK VALUE PIXELS}
}

Expand Down Expand Up @@ -327,7 +328,7 @@ command ttk::notebook {
.tnbid add .tnbid.a -text First
pack .tnbid
update
list [.tnbid identify tab 5 5] [.tnbid index @5,5]
list [.tnbid identify tab 20 12] [.tnbid index @20,12]
} {0 0}
error {ttk::notebook .tnbid2; .tnbid2 identify foo 1 1} {bad option "foo": must be element or tab} NONE
}
Expand Down
33 changes: 31 additions & 2 deletions docs/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,41 @@ TButton.Accent:hover { background-color: #1d4ed8; }

| Command | What it does in ztk |
|---|---|
| `configure STYLE ?-option VALUE ...?` | Adds a rule. `-foreground`, `-background`, `-fieldbackground`, `-bordercolor`, `-borderwidth`, `-relief`, `-padding`, `-font`, and `-indicatorcolor` change the look. Other options are stored for `lookup`. |
| `configure STYLE ?-option VALUE ...?` | Adds a rule. `-foreground`, `-background`, `-fieldbackground`, `-bordercolor`, `-borderwidth`, `-relief`, `-padding`, `-margin`, `-tabmargins` (of `TNotebook`), `-font`, and `-indicatorcolor` change the look. Other options are stored for `lookup`. |
| `map STYLE -option {STATES VALUE ...}` | Adds one rule for each pair. The first pair that matches wins, as in Tk. |
| `lookup STYLE -option ?STATE? ?DEFAULT?` | Gives the value, as `wish` gives it. |
| `lookup STYLE -option ?STATE? ?DEFAULT?` | Gives the value, as `wish` gives it. In the modern look, `-padding`, `-margin`, and `-tabmargins` give the value of the theme (see below). |
| `layout STYLE ?SPEC?` | Gives the layout of `wish`. A new layout is stored and has no effect on the look. |
| `element create`, `element names`, `element options` | As in `wish`. A new element has no effect on the look. |
| `theme create NAME ?-parent P? ?-settings SCRIPT?` | Makes a theme with the look of its parent. |
| `theme names`, `theme use ?NAME?`, `theme settings NAME SCRIPT` | List, select, and set up themes: `clam`, `alt`, `default`, `classic`, and yours. |

### The space of the theme

A theme of Tk puts space in some places, so a script with little `-padx` and `-pady` of its own still has room. The modern look puts space in the same places, with the sizes of the spacing scale of its sheet (`--space-1` to `--space-5`: 2, 4, 8, 12, and 16 pixels):

| Place | CSS | `ttk::style` | Modern look |
|---|---|---|---|
| The tabs and the client area of a notebook, from its edge | `TNotebook { padding }` | `TNotebook -padding` | `8 4 8 8` |
| Around the row of tabs | `TNotebook::tabs { padding }` | `TNotebook -tabmargins` | `4 0` |
| The page, from the edge of the client area | `TNotebook::client { padding }` | | 8, inside a border of 1 |
| Room for the focus ring of a button and a menu button | `TButton { margin }` | `TButton -margin` | `4 2` |
| The same for a check button and a radio button | `TCheckbutton { margin }` | `TCheckbutton -margin` | `2` |
| Around the text of a label | `TLabel { padding }` | `TLabel -padding` | `2` |
| The content of a label frame | `TLabelframe { padding }` | `TLabelframe -padding` | `8` |
| Room for the focus ring of a combobox | `TCombobox { margin }` | `TCombobox -margin` | `2` |
| The cells of a Treeview | `Treeview::item { padding }` | `Item -padding` | `8 4` |
| The headings of a Treeview | `Treeview::heading { padding }` | `Heading -padding` | `8 4` |

The classic widgets take their space from the same scale, and `cget` gives the value that the look draws when the script did not set the option:

| Widget | CSS | `-padx` | `-pady` | `-highlightthickness` |
|---|---|---|---|---|
| `button` | `Button { padding: var(--space-2) var(--space-4) }` | 12 | 4 | 2, the focus ring |
| `label` | no padding | 0 | 0 | 0 |
| `entry` | `Entry { padding: var(--space-2) var(--space-3) }` | | | 2, the focus ring |
| `checkbutton`, `radiobutton` | `Checkbutton { padding: var(--space-1) }` | 2 | 2 | 2, the focus ring |
| `labelframe` | `Labelframe { padding: var(--space-3) var(--space-4) }` | 12 | 8 | 0 |

The margin of a button or a combobox is inside its window, as in Tk: the request size grows, and the widget is drawn inside it. In a flex container the margin stays outside, as flex places it. The values are in the order of Tk: left, top, right, bottom. The `classic` theme keeps the values of `wish`.

The style `.` is the root: `ttk::style configure . -font {Inter 12}` applies to each `ttk` widget. [Differences from Tk](differences.md#styles-and-themes) has the rules that differ from `wish`.
Binary file modified examples/debug/golden-order.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion src/checks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ pub fn contrast(a: geom.Color, b: geom.Color) f32 {

/// The text of a widget, or an empty string for a class with no text.
fn textOf(w: *const Widget) []const u8 {
if (w.class.measure != label.measure and !std.mem.eql(u8, w.tk_class, "Entry")) return "";
// A button and a menu button of ttk wrap the measure of the label
// with their margin (ttk/widgets.zig).
const wraps = w.class.ttk and (std.mem.eql(u8, w.class.css_base, "Button") or std.mem.eql(u8, w.class.css_base, "Menubutton"));
if (w.class.measure != label.measure and !wraps and !std.mem.eql(u8, w.tk_class, "Entry")) return "";
if (std.mem.eql(u8, w.tk_class, "Entry")) return "x";
return label.text(w);
}
Expand Down
13 changes: 12 additions & 1 deletion src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub fn withHidden(comptime visible: []const Spec, comptime base: []const Spec) [
}
}

pub const StyleProp = enum { none, background, border_width };
/// `padx` and `pady` are the padding of the computed style, and
/// `focus_ring` is the width of its outline with the focus (ztk#1091).
pub const StyleProp = enum { none, background, border_width, padx, pady, focus_ring };

pub const Anchor = enum { n, ne, e, se, s, sw, w, nw, center };
pub const Relief = enum { flat, groove, raised, ridge, solid, sunken };
Expand Down Expand Up @@ -418,6 +420,15 @@ pub fn textOf(w: *Widget, it: *ztcl.Interp, i: usize) ztcl.Error![]const u8 {
return try it.alloc().dupe(u8, w.style.background.formatTk(&buf));
},
.border_width => return try std.fmt.allocPrint(it.alloc(), "{d}", .{@as(i32, @intFromFloat(@round(w.style.border_width[0])))}),
.padx => return try std.fmt.allocPrint(it.alloc(), "{d}", .{w.style.padding.left}),
.pady => return try std.fmt.allocPrint(it.alloc(), "{d}", .{w.style.padding.top}),
.focus_ring => {
// The style of the widget with the focus, as a probe.
var probe = w.*;
probe.state.focus = true;
const st = w.app.styles.compute(&probe, null, null, w.app.env);
return try std.fmt.allocPrint(it.alloc(), "{d}", .{@as(i32, @intFromFloat(@ceil(st.outline_width)))});
},
.none => {},
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/paint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,16 @@ fn bevel(b: *scene.Builder, r: geom.Rect, w: [4]i32, tl: geom.Color, br: geom.Co

/// Adds the shadow, the background, the border, and the focus outline.
pub fn box(w: *const Widget, b: *scene.Builder, style: *const Style) !void {
const r: geom.Rect = .{ .x = 0, .y = 0, .w = w.geom.w, .h = w.geom.h };
try boxIn(w, b, style, .{ .x = 0, .y = 0, .w = w.geom.w, .h = w.geom.h });
}

/// `box` in the rectangle `r` of the widget, such as the box inside the
/// margin of a combobox.
pub fn boxIn(w: *const Widget, b: *scene.Builder, style: *const Style, r: geom.Rect) !void {
const rad = radii(w, style);
if (style.shadow) |sh| try b.shadow(r, rad, sh);
try fillAndBorder(b, r, style, rad);
try outline(w, b, style);
try outlineIn(w, b, style, r);
}

/// True when the style draws a focus outline.
Expand All @@ -142,15 +147,18 @@ pub fn hasOutline(style: *const Style) bool {
/// Adds the focus outline of the widget, around its box, at the offset
/// of the builder.
pub fn outline(w: *const Widget, b: *scene.Builder, style: *const Style) !void {
try outlineIn(w, b, style, .{ .x = 0, .y = 0, .w = w.geom.w, .h = w.geom.h });
}

fn outlineIn(w: *const Widget, b: *scene.Builder, style: *const Style, r: geom.Rect) !void {
if (!hasOutline(style)) return;
const r: geom.Rect = .{ .x = 0, .y = 0, .w = w.geom.w, .h = w.geom.h };
const rad = radii(w, style);
const grow: i32 = ceilPx(style.outline_offset + style.outline_width);
const orad = @as(f32, @floatFromInt(grow));
var rr: [4]f32 = undefined;
for (rad, 0..) |v, i| rr[i] = v + orad;
const ow = style.outline_width;
try b.box(.{ .x = -grow, .y = -grow, .w = r.w + 2 * grow, .h = r.h + 2 * grow }, .{
try b.box(.{ .x = r.x - grow, .y = r.y - grow, .w = r.w + 2 * grow, .h = r.h + 2 * grow }, .{
.border = .{ ow, ow, ow, ow },
.border_color = style.outline_color,
.radii = rr,
Expand Down
4 changes: 4 additions & 0 deletions src/style.zig
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@ pub fn applyOptions(w: *const Widget, st: *Style) void {
if (std.mem.eql(u8, slot.text, "flat")) st.border_width = .{ 0, 0, 0, 0 };
}
} else if (std.mem.eql(u8, n, "-padding")) {
// The -padding of a notebook is around its pages, inside the
// client area (ttk/notebook.zig); its CSS padding is the space
// of the theme at its edge.
if (std.mem.eql(u8, w.class.command, "ttk::notebook")) continue;
if (parsePadding(slot.text)) |p| st.padding = p;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/tests/entry2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ test "entry2: the new options and the messages of wish" {
_ = try h.eval("entry .e");
const cases = [_][2][]const u8{
.{ ".e configure -disabledbackground", "-disabledbackground disabledBackground DisabledBackground #d9d9d9 #d9d9d9" },
.{ ".e configure -highlightthickness", "-highlightthickness highlightThickness HighlightThickness 1 1" },
// The default is the 1 of wish; the value is the width of the focus ring of the modern look (ztk#1091).
.{ ".e configure -highlightthickness", "-highlightthickness highlightThickness HighlightThickness 1 2" },
.{ ".e configure -invcmd", "-invalidcommand invalidCommand InvalidCommand {} {}" },
.{ ".e configure -readonlybackground", "-readonlybackground readonlyBackground ReadonlyBackground #d9d9d9 #d9d9d9" },
.{ ".e configure -validate", "-validate validate Validate none none" },
Expand Down
Loading
Loading