Skip to content

Commit

Permalink
demo: add checkbox for graying out a button
Browse files Browse the repository at this point in the history
Also add Color.average function
  • Loading branch information
david-vanderson committed Dec 11, 2024
1 parent 2a9dba9 commit e192685
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/Color.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ pub fn format(self: *const Color, comptime _: []const u8, _: std.fmt.FormatOptio
pub const white = Color{ .r = 0xff, .g = 0xff, .b = 0xff };
pub const black = Color{ .r = 0x00, .g = 0x00, .b = 0x00 };

/// Average two colors component-wise
pub fn average(self: Color, other: Color) Color {
return Color{
.r = @intCast((@as(u9, @intCast(self.r)) + other.r) / 2),
.g = @intCast((@as(u9, @intCast(self.g)) + other.g) / 2),
.b = @intCast((@as(u9, @intCast(self.b)) + other.b) / 2),
.a = @intCast((@as(u9, @intCast(self.a)) + other.a) / 2),
};
}

const clamp = std.math.clamp;

const FieldEnum = std.meta.FieldEnum(@This());
Expand Down
42 changes: 27 additions & 15 deletions src/Examples.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const enums = dvui.enums;
const zig_favicon = @embedFile("zig-favicon.png");

pub var show_demo_window: bool = false;
var checkbox_gray: bool = true;
var checkbox_bool: bool = false;
const RadioChoice = enum(u8) {
one = 1,
Expand Down Expand Up @@ -712,27 +713,38 @@ pub fn basicWidgets(demo_win_id: u32) !void {
_ = try dvui.button(@src(), "Button", .{}, .{ .gravity_y = 0.5 });
_ = try dvui.button(@src(), "Multi-line\nButton", .{}, .{});


{
var bw = ButtonWidget.init(@src(), .{}, .{ .gravity_y = 0.5 });
defer bw.deinit();
try bw.install();
bw.processEvents();
try bw.drawBackground();
try bw.drawFocus();
var vbox = try dvui.box(@src(), .vertical, .{});
defer vbox.deinit();

const opts = bw.data().options.strip().override(.{ .gravity_y = 0.5, .color_text = .{ .name = .fill_press } });
{
var color: ?dvui.Options.ColorOrName = null;
if (checkbox_gray) {
// blend text and control colors
color = .{ .color = dvui.Color.average(dvui.themeGet().color_text, dvui.themeGet().color_fill_control) };
}
var bw = dvui.ButtonWidget.init(@src(), .{}, .{ .gravity_y = 0.5, .color_text = color });
defer bw.deinit();
try bw.install();
bw.processEvents();
try bw.drawBackground();
try bw.drawFocus();

var bbox = try dvui.box(@src(), .horizontal, opts);
defer bbox.deinit();
const opts = bw.data().options.strip().override(.{ .gravity_y = 0.5 });

try dvui.icon(@src(), "cycle", entypo.cycle, opts);
_ = try dvui.spacer(@src(), .{ .w = 4 }, .{});
try dvui.labelNoFmt(@src(), "Icon+Gray", opts);
var bbox = try dvui.box(@src(), .horizontal, opts);
defer bbox.deinit();

if (bw.clicked()) {
try dvui.toast(@src(), .{ .subwindow_id = demo_win_id, .message = "This button is grayed out\nbut still clickable." });
try dvui.icon(@src(), "cycle", entypo.cycle, opts);
_ = try dvui.spacer(@src(), .{ .w = 4 }, .{});
try dvui.labelNoFmt(@src(), "Icon+Gray", opts);

if (bw.clicked()) {
try dvui.toast(@src(), .{ .subwindow_id = demo_win_id, .message = "This button is grayed out\nbut still clickable." });
}
}

_ = try dvui.checkbox(@src(), &checkbox_gray, "Gray", .{});
}
}

Expand Down

0 comments on commit e192685

Please sign in to comment.