summaryrefslogtreecommitdiffstats
path: root/gui/src/widgets.rs
blob: f7d15122706a7860b20c70830f4c3af790641480 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! Small shared controls. Every button and panel comes from here so the
//! screens cannot drift apart.

use eframe::egui::{
    Align, Button, Color32, Frame, Layout, Margin, RichText, Sense, Stroke, Ui, Vec2, vec2,
};

use crate::theme;

pub fn panel_frame() -> Frame {
    Frame::new()
        .fill(theme::SURFACE)
        .corner_radius(theme::RADIUS_LG)
        .inner_margin(Margin::same(theme::SPACE_4 as i8))
}

pub fn inset_frame() -> Frame {
    Frame::new()
        .fill(theme::SURFACE_RAISED)
        .corner_radius(theme::RADIUS_MD)
        .inner_margin(Margin::same(theme::SPACE_3 as i8))
}

/// The one green action on a screen.
pub fn primary(ui: &mut Ui, text: &str, enabled: bool, width: f32, height: f32) -> bool {
    let fill = if enabled {
        theme::WIN
    } else {
        theme::WIN.gamma_multiply(0.35)
    };
    let response = ui.add_enabled(
        enabled,
        Button::new(RichText::new(text).font(theme::mono(13.0)).color(theme::BG))
            .fill(fill)
            .corner_radius(theme::RADIUS_MD)
            .min_size(vec2(width, height)),
    );
    if response.hovered() {
        ui.painter().rect_filled(
            response.rect,
            theme::RADIUS_MD,
            Color32::WHITE.gamma_multiply(0.10),
        );
        ui.ctx()
            .set_cursor_icon(eframe::egui::CursorIcon::PointingHand);
    }
    response.clicked()
}

/// A bordered action that is not the main verb.
pub fn secondary(ui: &mut Ui, text: &str, enabled: bool, width: f32, height: f32) -> bool {
    let stroke = if enabled {
        theme::LINE
    } else {
        theme::LINE.gamma_multiply(0.5)
    };
    let response = ui.add_enabled(
        enabled,
        Button::new(
            RichText::new(text)
                .font(theme::mono(12.0))
                .color(theme::TEXT_DIM),
        )
        .fill(theme::SURFACE_RAISED)
        .stroke(Stroke::new(1.0, stroke))
        .corner_radius(theme::RADIUS_MD)
        .min_size(vec2(width, height)),
    );
    if response.hovered() {
        ui.painter().rect_filled(
            response.rect,
            theme::RADIUS_MD,
            Color32::WHITE.gamma_multiply(0.06),
        );
        ui.ctx()
            .set_cursor_icon(eframe::egui::CursorIcon::PointingHand);
    }
    response.clicked()
}

/// A quiet text action, for "back" and "forget".
pub fn ghost(ui: &mut Ui, text: &str) -> bool {
    let response = ui.add(
        Button::new(
            RichText::new(text)
                .font(theme::sans(12.0))
                .color(theme::TEXT_DIM),
        )
        .frame(false),
    );
    if response.hovered() {
        ui.painter().rect_filled(
            response.rect.expand(2.0),
            theme::RADIUS_SM,
            Color32::WHITE.gamma_multiply(0.05),
        );
        ui.ctx()
            .set_cursor_icon(eframe::egui::CursorIcon::PointingHand);
    }
    response.clicked()
}

/// Split a game sidebar into a flexible content area and a pinned action bar,
/// so the primary action never moves as the bet slip grows.
pub fn sidebar_layout(ui: &Ui, actions_height: f32) -> (eframe::egui::Rect, eframe::egui::Rect) {
    let full = ui.max_rect();
    let actions_top = (full.bottom() - actions_height).max(full.top());
    let actions = eframe::egui::Rect::from_min_max(
        eframe::egui::pos2(full.left(), actions_top),
        full.right_bottom(),
    );
    let content = eframe::egui::Rect::from_min_max(
        full.min,
        eframe::egui::pos2(full.right(), (actions_top - theme::SPACE_3).max(full.top())),
    );
    (content, actions)
}

pub fn square_button(ui: &mut Ui, text: &str, enabled: bool) -> bool {
    ui.add_enabled(
        enabled,
        Button::new(
            RichText::new(text)
                .font(theme::mono(16.0))
                .color(theme::TEXT),
        )
        .fill(theme::SURFACE_RAISED)
        .stroke(Stroke::new(1.0, theme::LINE))
        .corner_radius(theme::RADIUS_MD)
        .min_size(Vec2::splat(34.0)),
    )
    .clicked()
}

/// `- [ 50 Kr ] +`; returns the change in the bet.
pub fn bet_control(ui: &mut Ui, bet: u64, enabled: bool) -> Option<i64> {
    let mut delta = None;
    ui.horizontal(|ui| {
        if square_button(ui, "−", enabled) {
            delta = Some(-10);
        }
        Frame::new()
            .fill(theme::BG)
            .corner_radius(theme::RADIUS_SM)
            .inner_margin(Margin::symmetric(
                theme::SPACE_3 as i8,
                theme::SPACE_2 as i8,
            ))
            .show(ui, |ui| {
                ui.label(
                    RichText::new(theme::money(bet as i64))
                        .font(theme::mono(15.0))
                        .color(theme::TEXT),
                );
            });
        if square_button(ui, "+", enabled) {
            delta = Some(10);
        }
    });
    delta
}

/// Label on the left, value on the right, for slips and summaries.
pub fn row(ui: &mut Ui, label: &str, value: &str, color: Color32) {
    ui.horizontal(|ui| {
        ui.label(
            RichText::new(label)
                .font(theme::sans(13.0))
                .color(theme::TEXT_DIM),
        );
        ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
            ui.label(RichText::new(value).font(theme::mono(13.0)).color(color));
        });
    });
}

pub fn note(ui: &mut Ui, text: &str) {
    ui.label(
        RichText::new(text)
            .font(theme::sans(12.0))
            .color(theme::TEXT_DIM),
    );
}

pub fn error(ui: &mut Ui, text: &str) {
    Frame::new()
        .fill(theme::LOSS.gamma_multiply(0.12))
        .corner_radius(theme::RADIUS_SM)
        .inner_margin(Margin::symmetric(
            theme::SPACE_3 as i8,
            theme::SPACE_2 as i8,
        ))
        .show(ui, |ui| {
            ui.label(
                RichText::new(text)
                    .font(theme::sans(12.0))
                    .color(theme::LOSS),
            );
        });
}

/// A monospaced label that can be selected (fingerprints, addresses, URLs).
pub fn mono_text(ui: &mut Ui, text: &str, color: Color32) {
    ui.add(
        eframe::egui::Label::new(
            RichText::new(text)
                .font(theme::mono(theme::FONT_SMALL))
                .color(color),
        )
        .selectable(true),
    );
}

pub fn chip_button(ui: &mut Ui, amount: u64, selected: bool, enabled: bool) -> bool {
    let sense = if enabled {
        Sense::click()
    } else {
        Sense::hover()
    };
    let (rect, response) = ui.allocate_exact_size(Vec2::splat(46.0), sense);
    let center = rect.center();
    let radius = 20.0;
    let painter = ui.painter();
    if selected || response.hovered() {
        painter.circle_stroke(
            center,
            radius + 3.0,
            Stroke::new(if selected { 2.0 } else { 1.0 }, theme::ACCENT),
        );
    }
    if enabled {
        super::roulette::paint_chip(painter, center, radius, amount);
    } else {
        super::roulette::paint_chip_preview(painter, center, radius, amount);
    }
    response.clicked()
}

pub fn chip_row<const N: usize>(
    ui: &mut Ui,
    amounts: [u64; N],
    selected: u64,
    enabled: bool,
) -> Option<u64> {
    let mut picked = None;
    ui.horizontal(|ui| {
        ui.spacing_mut().item_spacing.x = theme::SPACE_2;
        for amount in amounts {
            if chip_button(ui, amount, selected == amount, enabled) {
                picked = Some(amount);
            }
        }
    });
    picked
}