Extract app preference planning into app core
This commit is contained in:
114
src/app_core/app_preferences.h
Normal file
114
src/app_core/app_preferences.h
Normal file
@@ -0,0 +1,114 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
|
||||
namespace pp::app {
|
||||
|
||||
enum class InterfaceDirection {
|
||||
left_to_right,
|
||||
right_to_left,
|
||||
};
|
||||
|
||||
enum class TimelapseRecordingAction {
|
||||
no_op,
|
||||
start_recording,
|
||||
stop_recording,
|
||||
};
|
||||
|
||||
struct ScaleApplicationPlan {
|
||||
float scale = 1.0F;
|
||||
float display_density = 1.0F;
|
||||
float font_scale = 1.0F;
|
||||
};
|
||||
|
||||
struct ScaleOptionSelection {
|
||||
bool has_selection = false;
|
||||
std::size_t index = 0;
|
||||
};
|
||||
|
||||
struct InterfaceDirectionPlan {
|
||||
InterfaceDirection direction = InterfaceDirection::left_to_right;
|
||||
};
|
||||
|
||||
struct TimelapsePreferencePlan {
|
||||
bool enabled = true;
|
||||
TimelapseRecordingAction recording_action = TimelapseRecordingAction::no_op;
|
||||
};
|
||||
|
||||
struct StoredIntegerPreferencePlan {
|
||||
int value = 0;
|
||||
};
|
||||
|
||||
struct StoredBooleanPreferencePlan {
|
||||
bool value = false;
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr ScaleApplicationPlan plan_ui_scale(
|
||||
float requested_scale,
|
||||
float display_density) noexcept
|
||||
{
|
||||
return {
|
||||
requested_scale,
|
||||
display_density,
|
||||
requested_scale * display_density,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr ScaleApplicationPlan plan_viewport_scale(
|
||||
float requested_scale,
|
||||
float display_density = 1.0F) noexcept
|
||||
{
|
||||
return {
|
||||
requested_scale,
|
||||
display_density,
|
||||
requested_scale * display_density,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr ScaleOptionSelection plan_scale_option_selection(
|
||||
float current_scale,
|
||||
std::span<const float> options) noexcept
|
||||
{
|
||||
ScaleOptionSelection selection;
|
||||
for (std::size_t index = 0; index < options.size(); ++index) {
|
||||
if (current_scale >= options[index]) {
|
||||
selection.has_selection = true;
|
||||
selection.index = index;
|
||||
}
|
||||
}
|
||||
return selection;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr InterfaceDirectionPlan plan_interface_direction(bool right_to_left) noexcept
|
||||
{
|
||||
return {
|
||||
right_to_left ? InterfaceDirection::right_to_left : InterfaceDirection::left_to_right,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr TimelapsePreferencePlan plan_timelapse_preference(
|
||||
bool enabled,
|
||||
bool recording_running) noexcept
|
||||
{
|
||||
if (enabled && !recording_running) {
|
||||
return { enabled, TimelapseRecordingAction::start_recording };
|
||||
}
|
||||
if (!enabled && recording_running) {
|
||||
return { enabled, TimelapseRecordingAction::stop_recording };
|
||||
}
|
||||
return { enabled, TimelapseRecordingAction::no_op };
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr StoredBooleanPreferencePlan plan_vr_controllers_preference(
|
||||
bool enabled) noexcept
|
||||
{
|
||||
return { enabled };
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr StoredIntegerPreferencePlan plan_canvas_cursor_mode(int mode) noexcept
|
||||
{
|
||||
return { mode };
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,12 +6,15 @@
|
||||
#include "node_progress_bar.h"
|
||||
#include "node_dialog_picker.h"
|
||||
#include "node_panel_floating.h"
|
||||
#include "app_core/app_preferences.h"
|
||||
#include "settings.h"
|
||||
#include "serializer.h"
|
||||
#include "font.h"
|
||||
#include "node_remote_page.h"
|
||||
#include "node_shorcuts.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
void App::title_update()
|
||||
{
|
||||
static char str[256];
|
||||
@@ -924,10 +927,14 @@ void App::init_menu_tools()
|
||||
|
||||
if (auto ui_scale = popup_time->find<NodeComboBox>("tools-ui-scale"))
|
||||
{
|
||||
// set index to current zoom level (or at least the closest in list)
|
||||
std::vector<float> scale_options;
|
||||
scale_options.reserve(ui_scale->m_data.size());
|
||||
for (int i = 0; i < ui_scale->m_data.size(); i++)
|
||||
if (App::I->zoom >= ui_scale->get_float(i))
|
||||
ui_scale->set_index(i);
|
||||
scale_options.push_back(ui_scale->get_float(i));
|
||||
|
||||
const auto selection = pp::app::plan_scale_option_selection(App::I->zoom, scale_options);
|
||||
if (selection.has_selection)
|
||||
ui_scale->set_index(static_cast<int>(selection.index));
|
||||
|
||||
ui_scale->on_select = [ui_scale](Node* target, int index)
|
||||
{
|
||||
@@ -937,16 +944,20 @@ void App::init_menu_tools()
|
||||
|
||||
if (auto vp_scale = popup_time->find<NodeComboBox>("tools-vp-scale"))
|
||||
{
|
||||
// set index to current zoom level (or at least the closest in list)
|
||||
std::vector<float> scale_options;
|
||||
scale_options.reserve(vp_scale->m_data.size());
|
||||
for (int i = 0; i < vp_scale->m_data.size(); i++)
|
||||
if (App::I->canvas->m_density >= vp_scale->get_float(i))
|
||||
vp_scale->set_index(i);
|
||||
scale_options.push_back(vp_scale->get_float(i));
|
||||
|
||||
const auto selection = pp::app::plan_scale_option_selection(App::I->canvas->m_density, scale_options);
|
||||
if (selection.has_selection)
|
||||
vp_scale->set_index(static_cast<int>(selection.index));
|
||||
|
||||
vp_scale->on_select = [vp_scale](Node* target, int index)
|
||||
{
|
||||
float d = vp_scale->get_float(index);
|
||||
App::I->canvas->set_density(d);
|
||||
Settings::set("vp-scale", Serializer::Float(d));
|
||||
const auto plan = pp::app::plan_viewport_scale(vp_scale->get_float(index));
|
||||
App::I->canvas->set_density(plan.scale);
|
||||
Settings::set("vp-scale", Serializer::Float(plan.scale));
|
||||
Settings::save();
|
||||
};
|
||||
}
|
||||
@@ -1010,8 +1021,9 @@ void App::init_menu_tools()
|
||||
|
||||
vr_btn->find<NodeCheckBox>("tools-vr-controllers-check")->on_value_changed = [this, main](Node* target, bool checked)
|
||||
{
|
||||
vr_controllers_enabled = checked;
|
||||
Settings::set("vr-controllers-enabled", Serializer::Boolean(checked));
|
||||
const auto plan = pp::app::plan_vr_controllers_preference(checked);
|
||||
vr_controllers_enabled = plan.value;
|
||||
Settings::set("vr-controllers-enabled", Serializer::Boolean(plan.value));
|
||||
Settings::save();
|
||||
};
|
||||
}
|
||||
@@ -1029,11 +1041,12 @@ void App::init_menu_tools()
|
||||
|
||||
btn->find<NodeCheckBox>("tools-timelapse-check")->on_value_changed = [this, main](Node*, bool checked)
|
||||
{
|
||||
if (!checked && App::I->rec_running)
|
||||
const auto plan = pp::app::plan_timelapse_preference(checked, App::I->rec_running);
|
||||
if (plan.recording_action == pp::app::TimelapseRecordingAction::stop_recording)
|
||||
App::I->rec_stop();
|
||||
else if (checked && !App::I->rec_running)
|
||||
else if (plan.recording_action == pp::app::TimelapseRecordingAction::start_recording)
|
||||
App::I->rec_start();
|
||||
Settings::set("auto-timelapse", Serializer::Boolean(checked));
|
||||
Settings::set("auto-timelapse", Serializer::Boolean(plan.enabled));
|
||||
Settings::save();
|
||||
};
|
||||
}
|
||||
@@ -1044,8 +1057,9 @@ void App::init_menu_tools()
|
||||
|
||||
mode->on_select = [mode](Node* target, int index)
|
||||
{
|
||||
App::I->canvas->set_cursor_visibility((NodeCanvas::kCursorVisibility)index);
|
||||
Settings::set("show-cursor", Serializer::Integer(index));
|
||||
const auto plan = pp::app::plan_canvas_cursor_mode(index);
|
||||
App::I->canvas->set_cursor_visibility((NodeCanvas::kCursorVisibility)plan.value);
|
||||
Settings::set("show-cursor", Serializer::Integer(plan.value));
|
||||
Settings::save();
|
||||
};
|
||||
}
|
||||
@@ -1418,17 +1432,20 @@ void App::initLayout()
|
||||
|
||||
void App::set_ui_scale(float scale)
|
||||
{
|
||||
zoom = scale;
|
||||
FontManager::change_scale(zoom * display_density);
|
||||
Settings::set("ui-scale", Serializer::Float(zoom));
|
||||
const auto plan = pp::app::plan_ui_scale(scale, display_density);
|
||||
zoom = plan.scale;
|
||||
FontManager::change_scale(plan.font_scale);
|
||||
Settings::set("ui-scale", Serializer::Float(plan.scale));
|
||||
Settings::save();
|
||||
App::I->title_update();
|
||||
}
|
||||
|
||||
void App::set_ui_rtl(bool rtl)
|
||||
{
|
||||
ui_rtl = rtl;
|
||||
layout[main_id]->find("central-row")->SetRTL(rtl ? YGDirectionRTL : YGDirectionLTR);
|
||||
const auto plan = pp::app::plan_interface_direction(rtl);
|
||||
ui_rtl = plan.direction == pp::app::InterfaceDirection::right_to_left;
|
||||
layout[main_id]->find("central-row")->SetRTL(
|
||||
ui_rtl ? YGDirectionRTL : YGDirectionLTR);
|
||||
}
|
||||
|
||||
bool App::get_ui_rtl() const
|
||||
|
||||
Reference in New Issue
Block a user