Consolidate frequency field on_edit (#1166)

This commit is contained in:
Kyle Reed
2023-06-18 12:11:04 -07:00
committed by GitHub
parent 7b669d7001
commit 5daa0dfbb1
36 changed files with 215 additions and 350 deletions

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2023 Kyle Reed
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __UI_FREQ_FIELD_H__
#define __UI_FREQ_FIELD_H__
#include "portapack.hpp"
#include "receiver_model.hpp"
#include "transmitter_model.hpp"
#include "ui_receiver.hpp"
namespace ui {
/* A frequency field bound directly a radio model
* that can manage its own editing UX. */
template <typename TModel, TModel* model>
class BoundFrequencyField : public FrequencyField {
private:
using FrequencyField::on_change;
using FrequencyField::on_edit;
public:
decltype(FrequencyField::on_change) updated{};
BoundFrequencyField(Point parent_pos, NavigationView& nav, bool update_model = true)
: FrequencyField(parent_pos) {
// NB: There is no frequency_step on the tx_model.
set_step(portapack::receiver_model.frequency_step());
set_value(model->target_frequency());
on_change = [this, update_model](rf::Frequency f) {
if (update_model)
model->set_target_frequency(f);
if (updated)
updated(f);
};
on_edit = [this, &nav]() {
auto freq_view = nav.push<FrequencyKeypadView>(model->target_frequency());
freq_view->on_changed = [this](rf::Frequency f) {
set_value(f);
};
};
}
// TODO: override set_step and update the rx model then call base.
};
using RxFrequencyField = BoundFrequencyField<ReceiverModel, &portapack::receiver_model>;
using TxFrequencyField = BoundFrequencyField<TransmitterModel, &portapack::transmitter_model>;
} // namespace ui
#endif // __UI_FREQ_FIELD_H__

View File

@@ -36,7 +36,6 @@
namespace ui {
// TODO: build in support for editing.
class FrequencyField : public Widget {
public:
std::function<void(rf::Frequency)> on_change{};
@@ -45,7 +44,7 @@ class FrequencyField : public Widget {
using range_t = rf::FrequencyRange;
FrequencyField(const Point parent_pos);
FrequencyField(Point parent_pos);
rf::Frequency value() const;
@@ -54,9 +53,9 @@ class FrequencyField : public Widget {
void paint(Painter& painter) override;
bool on_key(const ui::KeyEvent event) override;
bool on_encoder(const EncoderEvent delta) override;
bool on_touch(const TouchEvent event) override;
bool on_key(ui::KeyEvent event) override;
bool on_encoder(EncoderEvent delta) override;
bool on_touch(TouchEvent event) override;
void on_focus() override;
private: