Skip to content

Commit

Permalink
setting&autostart&widgets (OptionField and Waveform) imp (#2286)
Browse files Browse the repository at this point in the history
* test

* test

* format

* format

* tune order
  • Loading branch information
zxkmm authored Oct 6, 2024
1 parent 536d25d commit d4edb5f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
8 changes: 8 additions & 0 deletions firmware/application/apps/ui_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright (C) 2023 Kyle Reed
* Copyright (C) 2024 Mark Thompson
* Copyright (C) 2024 u-foka
* Copyright (C) 2024 HTotoo
* Copyleft (ɔ) 2024 zxkmm under GPL license
*
* This file is part of PortaPack.
Expand Down Expand Up @@ -876,6 +877,7 @@ SetAutostartView::SetAutostartView(NavigationView& nav) {
add_children({&labels,
&button_save,
&button_cancel,
&button_reset,
&options});

button_save.on_select = [&nav, this](Button&) {
Expand All @@ -892,6 +894,12 @@ SetAutostartView::SetAutostartView(NavigationView& nav) {
nav.pop();
};

button_reset.on_select = [this](Button&) {
selected = 0;
options.set_selected_index(0);
autostart_app = "";
};

// options
i = 0;
OptionsField::option_t o{"-none-", i};
Expand Down
18 changes: 12 additions & 6 deletions firmware/application/apps/ui_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,14 +843,19 @@ class SetAutostartView : public View {
"Save"};

OptionsField options{
{8 * 8, 4 * 16},
30,
{}};
{0 * 8, 4 * 16},
screen_width / 8,
{},
true};

Button button_cancel{
{16 * 8, 16 * 16, 12 * 8, 32},
"Cancel",
};

Button button_reset{
{2 * 8, 6 * 16, screen_width - 4 * 8, 32},
"Reset"};
};

class SetThemeView : public View {
Expand All @@ -872,15 +877,16 @@ class SetThemeView : public View {
"Save"};

OptionsField options{
{8 * 8, 4 * 16},
30,
{0 * 8, 4 * 16},
screen_width / 8,
{
{"Default - Grey", 0},
{"Yellow", 1},
{"Aqua", 2},
{"Green", 3},
{"Red", 4},
}};
},
true};

Checkbox checkbox_menuset{
{2 * 8, 6 * 16},
Expand Down
32 changes: 24 additions & 8 deletions firmware/application/ui_navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,30 @@ void NavigationView::handle_autostart() {
"nav"sv,
{{"autostart_app"sv, &autostart_app}}};
if (!autostart_app.empty()) {
if (StartAppByName(autostart_app.c_str())) return;
// if returned false, check for external apps by that name, and try to start it
std::string appwithpath = "/" + apps_dir.string() + "/";
appwithpath += autostart_app;
appwithpath += ".ppma";
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
std::filesystem::path pth = conv.from_bytes(appwithpath.c_str());
ui::ExternalItemsMenuLoader::run_external_app(*this, pth);
bool app_started = false;

// try innerapp
if (StartAppByName(autostart_app.c_str())) {
app_started = true;
} else {
// try outside app
auto external_items = ExternalItemsMenuLoader::load_external_items(app_location_t::HOME, *this);
for (const auto& item : external_items) {
if (item.text == autostart_app) {
item.on_select();
app_started = true;
break;
}
}
}

if (!app_started) {
display_modal(
"Notice", "Autostart failed:\n" +
autostart_app +
"\nupdate sdcard content\n" +
"and check if .ppma exists");
}
}
}

Expand Down
28 changes: 22 additions & 6 deletions firmware/common/ui_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,10 +1697,12 @@ bool ImageOptionsField::on_touch(const TouchEvent event) {
OptionsField::OptionsField(
Point parent_pos,
size_t length,
options_t options)
options_t options,
bool centered)
: Widget{{parent_pos, {8 * (int)length, 16}}},
length_{length},
options_{std::move(options)} {
options_{std::move(options)},
centered_{centered} {
set_focusable(true);
}

Expand Down Expand Up @@ -1792,8 +1794,19 @@ void OptionsField::paint(Painter& painter) {
std::string_view temp = selected_index_name();
if (temp.length() > length_)
temp = temp.substr(0, length_);

Point draw_pos = screen_pos();
if (centered_) {
// 8 is because big font width is 8px
// type is from: struct Point : constexpr int x() const
int text_width = temp.length() * 8;
int available_width = length_ * 8;
int x_offset = (available_width - text_width) / 2;
draw_pos = {draw_pos.x() + x_offset, draw_pos.y()};
}

painter.draw_string(
screen_pos(),
draw_pos,
paint_style,
temp);
}
Expand Down Expand Up @@ -2622,6 +2635,8 @@ void Waveform::set_length(const uint32_t new_length) {
}

void Waveform::paint(Painter& painter) {
// previously it's upside down , low level is up and high level is down, which doesn't make sense,
// if that was made for a reason, feel free to revert.
size_t n;
Coord y, y_offset = screen_rect().location().y();
Coord prev_x = screen_rect().location().x(), prev_y;
Expand All @@ -2642,7 +2657,7 @@ void Waveform::paint(Painter& painter) {
x = 0;
h--;
for (n = 0; n < length_; n++) {
y = *(data_start++) ? h : 0;
y = *(data_start++) ? 0 : h;

if (n) {
if (y != prev_y)
Expand All @@ -2658,9 +2673,10 @@ void Waveform::paint(Painter& painter) {
// Analog waveform: each value is a point's Y coordinate
x = prev_x + x_inc;
h /= 2;
prev_y = y_offset + h - (*(data_start++) * y_scale);

prev_y = y_offset + h + (*(data_start++) * y_scale);
for (n = 1; n < length_; n++) {
y = y_offset + h - (*(data_start++) * y_scale);
y = y_offset + h + (*(data_start++) * y_scale);
display.draw_line({prev_x, prev_y}, {(Coord)x, y}, color_);

prev_x = x;
Expand Down
5 changes: 3 additions & 2 deletions firmware/common/ui_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ class OptionsField : public Widget {
std::function<void(size_t, value_t)> on_change{};
std::function<void(void)> on_show_options{};

OptionsField(Point parent_pos, size_t length, options_t options);
OptionsField(Point parent_pos, size_t length, options_t options, bool centered = false);

options_t& options() { return options_; }
const options_t& options() const { return options_; }
Expand Down Expand Up @@ -708,6 +708,7 @@ class OptionsField : public Widget {
const size_t length_;
options_t options_;
size_t selected_index_{0};
bool centered_{false}; // e.g.: length as screen_width/8, x position as 0, it will be centered in x axis
};

// A TextEdit is bound to a string reference and allows the string
Expand Down Expand Up @@ -1036,4 +1037,4 @@ class OptionTabView : public View {

} /* namespace ui */

#endif /*__UI_WIDGET_H__*/
#endif /*__UI_WIDGET_H__*/

0 comments on commit d4edb5f

Please sign in to comment.