Files
breeze/src/screens/settings/settings_theme.zi
T

538 lines
20 KiB
Ziran

#import, file "../../app/modal_rules.zi";
#import, file "../../app/app_types.zi";
#import, file "settings_types.zi";
#import "geometry"
#import "text_props"
#import "text_align"
#import "image_props"
#import "control_props"
#import "layout"
#import "scroll"
#import "scroll_props"
#import "scroll_widget"
#import "tree"
#import "tree_input"
#import "modal_frame_widget"
#import "modal_props"
#import, file "../../app/app.zi";
#import, file "../../app/app_nav_state.zi";
#import, file "../../app/app_settings.zi";
#import, file "../../app/app_style.zi";
#import, file "../../app/app_modal_state.zi";
#import, file "../../app/widgets.zi";
#import, file "../../app/theme_catalog.zi";
#import, file "../../app/text_buffers.zi";
#import, file "../../app/device_preferences.zi";
#import, file "../../app/locale.zi";
#import, file "settings_ui.zi";
#import, file "settings_appearance.zi";
#import "input_props"
#import, file "../../app/metrics.zi";
#import, file "../../app/current_app.zi";
#import "button_widget"
#import "dropdown_widget"
#import "image_widget"
#import "segmented_control_widget"
#import "slider_widget"
#import "slider"
#import "text_widget"
#import "drawing_props"
#import "session"
#import "button_props"
#import "dropdown_props"
#import "segmented_control_props"
#import "slider_props"
using AppThemeMode;
using AppThemeSource;
using ModalType;
using TextWrap;
using TextAlign;
using ImageFit;
using ButtonTone;
using ButtonEmphasis;
ANDROID :: #defined(ANDROID_BUILD)
WEB :: #defined(PLATFORM_WEB)
#if ANDROID {
c :: #system_library "c";
android_device_app_icon :: () -> s32 #foreign c;
android_device_set_app_icon :: (icon: s32) -> s32 #foreign c;
}
settings_theme_scale_label_height :: (width: s32) -> s32 {
height := AppParagraphHeight(LocaleText("appearance_scale_label"),
width, AppFontSize(), 0)
if height < AppScale(22) { height = AppScale(22) }
return height
}
settings_theme_scale_stacked :: (width: s32) -> bool {
return AppTextWidth(LocaleText("appearance_scale_label"), AppFontSize()) +
AppScale(80) > width
}
settings_theme_scale_row_height :: (width: s32) -> s32 {
height := AppScale(86)
if settings_theme_scale_stacked(width) {
height += settings_theme_scale_label_height(width) + AppScale(6)
}
return height
}
settings_theme_icon_row_height :: () -> s32 {
return AppScale(72)
}
settings_theme_theme_body :: (app: *InnerBreeze, width: s32) -> s32 {
height: s32 = 0
#if ANDROID {
height += settings_theme_icon_row_height()
}
height += settings_choice_row_height(LocaleText("appearance_style_label"),
width)
if style_pack_has_glow(app.style_index) != 0 {
height += toggle_row_height(LocaleText("appearance_glow_effects_label"),
width)
}
height += settings_choice_row_height(LocaleText("theme_color_label"), width)
height += settings_choice_row_height(LocaleText("theme_mode_label"), width)
return height
}
settings_theme_layout_body :: (app: *InnerBreeze, width: s32) -> s32 {
height := settings_theme_scale_row_height(width)
height += settings_choice_row_height(LocaleText("navigation_placement"),
width)
return height
}
settings_theme_content_height :: (app: *InnerBreeze, content_w: s32) -> s32 {
inner := settings_section_inner_width(content_w)
return settings_section_height(LocaleText("theme_label"),
settings_theme_theme_body(app, inner)) +
settings_section_height(LocaleText("settings_section_layout"),
settings_theme_layout_body(app, inner)) + settings_appearance_height(inner)
}
settings_theme_draw_app_icon :: (app: *InnerBreeze, section: SettingsSection,
y: *s32, state: *SettingsThemeState) {
#if ANDROID {
if state.app_icon_loaded == 0 {
state.app_icon = android_device_app_icon()
state.app_icon_loaded = 1
}
row_h := settings_theme_icon_row_height()
icon_size := AppScale(48)
if Button(app.ui.session, ButtonProps.{
.id = 152,
.bounds = Rectangle.{cast(float32)section.inner_x,
cast(float32)(y.* + AppScale(6)),
cast(float32)section.inner_width,
cast(float32)(row_h - AppScale(12))},
.label = LocaleText("app_icon_label"),
.tone = cast(ButtonTone)ButtonToneNeutral,
.emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft,
.disabled = app.modal.active != 0
}) != 0 {
state.app_icon = android_device_app_icon()
state.app_icon_error = 0
app_open_modal(app, cast(ModalType)ModalAppIconPicker)
}
Image(app.ui.session, ImageProps.{
.asset_path = ifx (state.app_icon == 1) then ("assets/app/icon-ink-and-air.webp") else ("assets/app/icon-sky-cradle.webp"),
.bounds = Rectangle.{cast(float32)(section.inner_x + AppScale(8)),
cast(float32)(y.* + (row_h - icon_size) / 2),
cast(float32)icon_size, cast(float32)icon_size},
.fit = cast(ImageFit)ImageFitContain
})
y.* += row_h
settings_row_divider(app, section, y.*)
} else {
unused app
unused section
unused y
unused state
}
}
settings_theme_draw :: (app: *InnerBreeze, x: s32, w: s32,
y: *s32, state: *SettingsThemeState) {
if app == null || y == null || state == null {
return
}
inner := settings_section_inner_width(w)
theme := settings_section_begin(app, x, w, y, LocaleText("theme_label"),
settings_theme_theme_body(app, inner))
settings_theme_draw_app_icon(app, theme, y, state)
styles: [3]DropdownOption
styles[0].label = style_pack_label(1)
styles[1].label = style_pack_label(2)
styles[2].label = style_pack_label(3)
style_props: DropdownProps
style_props.id = 150
style_props.bounds = settings_choice_row(app, theme.inner_x,
theme.inner_width, y, LocaleText("appearance_style_label"))
style_props.selected_index = app.style_index - 1
style_menu := Dropdown(app.ui.session, style_props, styles[:])
if style_menu.changed {
app.style_index = style_menu.selected_index + 1
app_apply_style(app)
app.settings_scroll = 0
app_block_current_click(app)
save_settings(app)
}
if style_pack_has_glow(app.style_index) != 0 {
settings_row_divider(app, theme, y.*)
if commit_toggle_row(app, theme.inner_x, theme.inner_width, y,
LocaleText("appearance_glow_effects_label"),
*app.lightfield_glow_enabled, 1) != 0 {
app_apply_style(app)
}
}
settings_row_divider(app, theme, y.*)
if draw_theme_settings(app, theme, y, state) != 0 {
app_refresh_theme(app)
app.settings_dirty = 1
app.settings_save_delay_ticks = 18
}
settings_section_end(y)
layout := settings_section_begin(app, x, w, y,
LocaleText("settings_section_layout"),
settings_theme_layout_body(app, inner))
draw_scale_row(app, layout.inner_x, layout.inner_width, y, state)
settings_row_divider(app, layout, y.*)
placements: [5]DropdownOption
placements[0].label = LocaleText("navigation_automatic")
placements[1].label = LocaleText("navigation_left")
placements[2].label = LocaleText("navigation_bottom")
placements[3].label = LocaleText("navigation_right")
placements[4].label = LocaleText("navigation_top")
placement_props: DropdownProps
placement_props.id = 149
placement_props.bounds = settings_choice_row(app, layout.inner_x,
layout.inner_width, y, LocaleText("navigation_placement"))
placement_props.selected_index = app.navigation_placement
placement_menu := Dropdown(app.ui.session, placement_props, placements[:])
if placement_menu.changed {
app.navigation_placement = placement_menu.selected_index
app.settings_scroll = 0
app_block_current_click(app)
save_settings(app)
}
settings_section_end(y)
settings_appearance_draw(app, x, w, y, state)
}
#program_export
settings_screen_draw_app_icon_modal :: (app: *InnerBreeze) {
#if ANDROID {
state := *app.theme_state
frame := ModalFrame(app.ui.session, ModalFrameProps.{
.key = KeyHash("settings/app-icon"),
.title = LocaleText("app_icon_label"),
.width = AppScale(360), .height = AppScale(300),
.scale = MetricsScale(),
.has_right_action = true,
.right_label = LocaleText("close_button")})
if frame.dismissed || frame.right_clicked {
app_close_modal(app)
return
}
if !frame.opened {
return
}
content := frame.layout.content
content_x := cast(s32)content.x
content_y := cast(s32)content.y
content_w := cast(s32)content.width
names: [2]string = .[
LocaleText("app_icon_sky_cradle"),
LocaleText("app_icon_ink_and_air")]
previews: [2]string = .[
"assets/app/icon-sky-cradle.webp",
"assets/app/icon-ink-and-air.webp"]
gap := AppScale(12)
choice_w := (content_w - gap) / 2
{
i: s32 = 0
while i < 2 {
choice_x := content_x + i * (choice_w + gap)
selected := state.app_icon == i
Image(CurrentApp().ui.session, ImageProps.{
.asset_path = previews[i], .alt_text = names[i],
.bounds = Rectangle.{cast(float32)choice_x,
cast(float32)content_y, cast(float32)choice_w,
cast(float32)AppScale(120)},
.fit = cast(ImageFit)ImageFitContain
})
if Button(CurrentApp().ui.session, ButtonProps.{
.id = cast(s32)153 + i,
.bounds = Rectangle.{cast(float32)choice_x,
cast(float32)(content_y + AppScale(128)),
cast(float32)choice_w, cast(float32)AppScale(44)},
.label = names[i],
.tone = ifx selected then cast(ButtonTone)ButtonToneAccent else cast(ButtonTone)ButtonToneNeutral,
.emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft
}) != 0 {
state.app_icon_error = ifx android_device_set_app_icon(i) == 0 then 1 else 0
state.app_icon = android_device_app_icon()
app_block_current_click(app)
if state.app_icon_error == 0 {
unused End(app.ui.session)
app_close_modal(app)
return
}
}
i += 1
}
}
message := LocaleText(ifx state.app_icon_error != 0 then "app_icon_error" else "app_icon_hint")
Text(CurrentApp().ui.session, TextProps.{
.bounds = Rectangle.{cast(float32)content_x,
cast(float32)(content_y + AppScale(184)),
cast(float32)content_w, cast(float32)AppScale(30)},
.text = message, .wrap = cast(TextWrap)TextWrapNone,
.font = AppFitFontSize(message, content_w,
AppSmallFontSize(), AppScale(8))
})
unused End(app.ui.session)
} else {
unused app
}
}
ThemeLabel :: (id: s32) -> string {
if id == 0 { return LocaleText("theme_sky") }
if id == 1 { return LocaleText("theme_ocean") }
if id == 2 { return LocaleText("theme_forest") }
if id == 3 { return LocaleText("theme_sunset") }
if id == 4 { return LocaleText("theme_lavender") }
if id == 5 { return LocaleText("theme_cherry") }
if id == 6 { return LocaleText("theme_dawn") }
if id == 7 { return LocaleText("theme_sage") }
if id == 8 { return LocaleText("theme_sepia") }
if id == 9 { return LocaleText("theme_mono") }
if id == 10 { return LocaleText("theme_mint") }
if id == 11 { return LocaleText("theme_cobalt") }
if id == 12 { return "Sweet" }
return LocaleText("theme_mono")
}
draw_theme_settings :: (app: *InnerBreeze, section: SettingsSection,
y: *s32, state: *SettingsThemeState) -> s32 {
changed: s32 = 0
theme_options: [ThemeCount + 1]DropdownOption
theme_options[0].label = LocaleText("theme_system")
{
i: s32 = 0
while i < ThemeCount {
theme_options[i + 1].label = ThemeLabel(i)
i += 1
}
}
theme_index := ifx (app.theme_source == APP_THEME_SOURCE_SYSTEM) then (0) else (app.theme_id + 1)
if theme_index < 0 || theme_index > ThemeCount {
theme_index = 1
}
color_props: DropdownProps
color_props.id = 103
color_props.bounds = settings_choice_row(app, section.inner_x,
section.inner_width, y, LocaleText("theme_color_label"))
color_props.selected_index = theme_index
color_menu := Dropdown(app.ui.session, color_props, theme_options[:])
if color_menu.changed {
if color_menu.selected_index == 0 {
app.theme_source = APP_THEME_SOURCE_SYSTEM
app.theme_mode = APP_THEME_SYSTEM
} else {
app.theme_source = APP_THEME_SOURCE_APP
app.theme_id = color_menu.selected_index - 1
}
changed = 1
}
settings_row_divider(app, section, y.*)
// The dropdown paints after this function returns, so the formatted
// label must live in frame text rather than on this stack.
follow_device_label: [128]u8
follow_args: [2]FormatArg = .[
FormatArg.{.text = LocaleText("theme_follow_device"), .is_text = true},
FormatArg.{.text = LocaleText(ifx app.dark_mode != 0 then "theme_dark" else "theme_light"), .is_text = true}
]
FormatTemplate("%s (%s)", follow_args[:], follow_device_label[:])
mode_options: [3]DropdownOption
mode_options[0].label = FrameTextHold(*app.ui.frame_text,
AppTextFromCString(*follow_device_label[0],
size_of(type_of(follow_device_label))))
mode_options[1].label = LocaleText("theme_light")
mode_options[2].label = LocaleText("theme_dark")
mode_index := app.theme_mode
if mode_index < APP_THEME_SYSTEM || mode_index > APP_THEME_DARK {
mode_index = APP_THEME_SYSTEM
}
mode_props: DropdownProps
mode_props.id = 102
mode_props.bounds = settings_choice_row(app, section.inner_x,
section.inner_width, y, LocaleText("theme_mode_label"))
mode_props.selected_index = mode_index
mode_props.disabled = app.theme_source == APP_THEME_SOURCE_SYSTEM
mode_menu := Dropdown(app.ui.session, mode_props, mode_options[:])
if mode_menu.changed {
app.theme_mode = mode_menu.selected_index
changed = 1
}
return changed
}
draw_scale_row :: (app: *InnerBreeze, x: s32, w: s32, y: *s32,
state: *SettingsThemeState) {
if !state.scale_editing {
state.scale_preview = app.ui_scale_tenths
}
label := LocaleText("appearance_scale_label")
percentage: [24]u8
percentage_args: [1]FormatArg = .[
FormatArg.{.number = state.scale_preview * 10}
]
FormatTemplate("%d%%", percentage_args[:], percentage[:])
value := FrameTextHold(*app.ui.frame_text,
AppTextFromCString(*percentage[0], size_of(type_of(percentage))))
value_width := AppScale(64)
label_height := AppScale(22)
label_width := w - value_width
value_y := y.* + AppScale(10)
slider_y := y.* + AppScale(38)
if settings_theme_scale_stacked(w) {
label_width = w
label_height = settings_theme_scale_label_height(w)
value_y += label_height + AppScale(6)
slider_y += label_height + AppScale(6)
}
Text(app.ui.session, TextProps.{
.bounds = Rectangle.{cast(float32)x, cast(float32)(y.* + AppScale(10)),
cast(float32)label_width, cast(float32)label_height},
.text = label,
.wrap = cast(TextWrap)TextWrapAuto
})
Text(app.ui.session, TextProps.{
.bounds = Rectangle.{cast(float32)(x + w - value_width),
cast(float32)value_y,
cast(float32)value_width, cast(float32)AppScale(22)},
.text = value,
.align = cast(TextAlign)TextAlignEnd,
.wrap = cast(TextWrap)TextWrapNone
})
slider: DiscreteSliderProps
slider.id = 140
slider.bounds = Rectangle.{cast(float32)x,
cast(float32)slider_y, cast(float32)w,
cast(float32)AppScale(36)}
slider.semantic_label = label
slider.value = state.scale_preview
slider.min = 5
slider.max = 25
result := DiscreteSlider(app.ui.session, slider)
if result.changed {
state.scale_preview = result.value
state.scale_editing = true
}
// Keep the slider, hit targets and responsive sidebars fixed while held.
// Apply the chosen zoom once the gesture ends, including focus loss.
pointer := PointerSample(app.ui.session)
if state.scale_editing && !pointer.down {
state.scale_editing = false
app.ui_scale_tenths = state.scale_preview
app.settings_dirty = 1
app_block_current_click(app)
save_settings(app)
}
y.* += settings_theme_scale_row_height(w)
}
#program_export
settings_screen_draw_theme_picker_modal :: (app: *InnerBreeze) {
modal_w := AppScale(320)
modal_h := AppScale(360)
title := LocaleText("theme_picker_title")
frame := ModalFrame(app.ui.session, ModalFrameProps.{
.key = KeyHash("settings/theme-picker"),
.title = title, .width = modal_w, .height = modal_h,
.scale = MetricsScale(),
.has_right_action = true,
.right_label = LocaleText("close_button")})
if frame.dismissed || frame.right_clicked {
app_close_modal(app)
return
}
if !frame.opened {
return
}
content := frame.layout.content
picker_h := theme_picker_content_height(cast(s32)content.width)
{
scroll_1_bounds: Rectangle = content
scroll_1_key: u64 = KeyHash("inbe/screens/settings/settings_theme/scroll/1")
scroll_1_props: ScrollProps
scroll_1_props.key = scroll_1_key
scroll_1_props.bounds = scroll_1_bounds
scroll_1_props.content_height = picker_h
scroll_1_props.scroll_offset = app.theme_state.theme_picker_scroll
scroll_1_result: ScrollResult = Scroll(CurrentApp().ui.session, scroll_1_props)
app.theme_state.theme_picker_scroll = scroll_1_result.scroll_offset
picker: Rectangle = scroll_1_result.content
if draw_theme_picker(cast(s32)picker.x, cast(s32)picker.y,
cast(s32)picker.width, app.dark_mode, *app.theme_id) != 0 {
if app.theme_id < 0 { app.theme_id = 0 }
if app.theme_id >= ThemeCount { app.theme_id = ThemeCount - 1 }
app.theme_source = APP_THEME_SOURCE_APP
app_refresh_theme(app)
app.settings_dirty = 1
save_settings(app)
app_close_modal(app)
}
unused End(app.ui.session)
}
unused End(app.ui.session)
}
theme_picker_content_height :: (w: s32) -> s32 {
unused w
return ThemeCount * AppScale(46)
}
draw_theme_picker :: (x: s32, y: s32, w: s32, dark_mode: s32, theme_id: *s32) -> s32 {
unused dark_mode
selected: s32 = 0
if theme_id != null { selected = theme_id.* }
changed: s32 = 0
{
i: s32 = 0
while i < ThemeCount {
if Button(CurrentApp().ui.session, ButtonProps.{
.id = cast(s32)51000 + i,
.bounds = Rectangle.{cast(float32)x, cast(float32)(y + i * AppScale(46)),
cast(float32)w, cast(float32)AppScale(40)},
.label = ThemeLabel(i),
.tone = ifx (selected == i) then cast(ButtonTone)ButtonToneAccent else cast(ButtonTone)ButtonToneNeutral,
.emphasis = ifx (selected == i) then cast(ButtonEmphasis)ButtonEmphasisFilled else cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 {
if theme_id != null {
theme_id.* = i
}
changed = 1
}
i += 1
}
}
return changed
}