refactor: Clean up VolSurfaceRowVM
* use new lang feats * run formatter
This commit is contained in:
1 parent
abfabe3e21
commit
05348b01ae
1 file changed
+33
-44
@@ -1,3 +1,4 @@
|
||||
#nullable enable
|
||||
namespace mbts.spark.VolSurface {
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
@@ -10,21 +11,17 @@ namespace mbts.spark.VolSurface {
|
||||
using System.Windows.Media;
|
||||
|
||||
public struct VolSurfaceCellState {
|
||||
public bool is_dirty => committed_text != null;
|
||||
public FormattedText text;
|
||||
public FormattedText committed_text;
|
||||
public bool is_dirty => committed_text is not null;
|
||||
public FormattedText? text;
|
||||
public FormattedText? committed_text;
|
||||
}
|
||||
|
||||
class VolSurfaceRowVM : DisposableViewModelBase, ISelectable, IFormatCellsToClipboard, DIYGrid.IRowVM {
|
||||
readonly VolSurfaceGridVM _parent;
|
||||
readonly VolSurfaceDisplayGridExpiration _exp;
|
||||
/// TODO: remove _text_cache when we are sure that the new row view
|
||||
/// (<see cref="VolSurfaceRowView"/>) is solid. It must remain in place
|
||||
/// until then, as it is used by old row view
|
||||
/// (<see cref="VolSurfaceRowControl"/>).
|
||||
readonly Dictionary<string, CellData> _cells = new();
|
||||
|
||||
VolSurfaceRowContextMenuVM _context_menu;
|
||||
VolSurfaceRowContextMenuVM? _context_menu;
|
||||
bool _is_selected;
|
||||
|
||||
public VolSurfaceRowVM(VolSurfaceGridVM parent, VolSurfaceDisplayGridExpiration exp) {
|
||||
@@ -36,16 +33,15 @@ namespace mbts.spark.VolSurface {
|
||||
});
|
||||
}
|
||||
|
||||
public event Action<bool> is_selected_changed;
|
||||
public event Action invalidated;
|
||||
public event Action<bool>? is_selected_changed;
|
||||
public event Action? invalidated;
|
||||
|
||||
public VolSurfaceDisplayGridExpiration expiration => _exp;
|
||||
|
||||
public bool is_selected {
|
||||
get => _is_selected;
|
||||
set {
|
||||
if (_is_selected != value) {
|
||||
_is_selected = value;
|
||||
if (Set(ref _is_selected, value)) {
|
||||
is_selected_changed?.Invoke(value);
|
||||
_parent.signal_selection_changed();
|
||||
}
|
||||
@@ -55,34 +51,28 @@ namespace mbts.spark.VolSurface {
|
||||
public VolSurfaceRowContextMenuVM context_menu =>
|
||||
_context_menu ??= new(_parent, this);
|
||||
|
||||
public VolGridCellVM get_cell(VolSurfaceDisplayGridColumn column) {
|
||||
if (!_cells.TryGetValue(column.unique_col_name, out var cell_data)) {
|
||||
_cells.Add(column.unique_col_name, cell_data = _create_cell(column));
|
||||
}
|
||||
return cell_data.vm;
|
||||
}
|
||||
public VolGridCellVM get_cell(VolSurfaceDisplayGridColumn column) =>
|
||||
_cells.GetOrAdd(column.unique_col_name, _ => _create_cell(column)).vm;
|
||||
|
||||
public void invalidate() {
|
||||
foreach (var cell in _cells.Values) {
|
||||
_sync_cell(cell.vm);
|
||||
}
|
||||
raise_invalidated();
|
||||
invalidated?.Invoke();
|
||||
}
|
||||
|
||||
public override string ToString() =>
|
||||
_exp.ToString();
|
||||
|
||||
public string[] get_cell_clipboard_strings() =>
|
||||
new[] { _exp.ToString() }.Concat(
|
||||
_parent.columns.OfType<VolSurfaceColumn>()
|
||||
.Skip(1)
|
||||
.Select(c => {
|
||||
_parent.columns.OfType<VolSurfaceColumn>().Skip(1).Select(c => {
|
||||
var val = _parent.current_grid.read(_exp, c.model.unique_col_name);
|
||||
return c.model.format(val);
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
public void raise_invalidated() =>
|
||||
public override string ToString() =>
|
||||
_exp.ToString();
|
||||
|
||||
void DIYGrid.IRowVM.raise_invalidated() =>
|
||||
invalidated?.Invoke();
|
||||
|
||||
CellData _create_cell(VolSurfaceDisplayGridColumn column) {
|
||||
@@ -91,15 +81,16 @@ namespace mbts.spark.VolSurface {
|
||||
_ => new VolGridCellVM(column.unique_col_name),
|
||||
};
|
||||
_sync_cell(cell);
|
||||
var dispose = cell.observe_candidate().DistinctUntilChanged().OnDispatcher().Subscribe(value => {
|
||||
var curr = _parent.current_grid.read(expiration, column);
|
||||
if (Equals(value, curr)) {
|
||||
return;
|
||||
}
|
||||
var grid = _parent.current_grid;
|
||||
grid.write(expiration, column, value);
|
||||
_parent.model.set_candidate_grid(grid);
|
||||
});
|
||||
var dispose = cell.observe_candidate().DistinctUntilChanged().OnDispatcher()
|
||||
.Subscribe(value => {
|
||||
var curr = _parent.current_grid.read(expiration, column);
|
||||
if (Equals(value, curr)) {
|
||||
return;
|
||||
}
|
||||
var grid = _parent.current_grid;
|
||||
grid.write(expiration, column, value);
|
||||
_parent.model.set_candidate_grid(grid);
|
||||
});
|
||||
return new(cell, dispose);
|
||||
|
||||
VolGridTickCellVM create_tick_cell() {
|
||||
@@ -107,10 +98,11 @@ namespace mbts.spark.VolSurface {
|
||||
cell.request_tick += is_up => {
|
||||
var col = _parent.get_column(column.unique_col_name);
|
||||
if (col is null) {
|
||||
throw new InvalidOperationException($"Failed to find column with id {column.unique_col_name}");
|
||||
throw new InvalidOperationException(
|
||||
$"Failed to find column with id {column.unique_col_name}");
|
||||
}
|
||||
if (col.model?.tick_move is var tm) {
|
||||
cell.candidate_value = tm(cell.candidate_value, is_up);
|
||||
cell.candidate_value = tm?.Invoke(cell.candidate_value, is_up);
|
||||
return;
|
||||
}
|
||||
throw new InvalidOperationException($"tick_move is not defined");
|
||||
@@ -129,19 +121,16 @@ namespace mbts.spark.VolSurface {
|
||||
|
||||
class CellData : IDisposable {
|
||||
IDisposable _dispose;
|
||||
VolGridCellVM _vm;
|
||||
|
||||
public CellData(VolGridCellVM vm, IDisposable dispose) {
|
||||
_vm = vm;
|
||||
_dispose = dispose;
|
||||
this.vm = vm;
|
||||
_dispose = Disposable.Create(vm, dispose);
|
||||
}
|
||||
|
||||
public VolGridCellVM vm => _vm;
|
||||
public VolGridCellVM vm { get; }
|
||||
|
||||
public void Dispose() {
|
||||
Disposable.dispose(ref _vm);
|
||||
public void Dispose() =>
|
||||
Disposable.dispose(ref _dispose);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user