This repository was archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
[Mac] Cache cell width to improve size calculations #813
Open
sevoku
wants to merge
3
commits into
main
Choose a base branch
from
mac-improve-cell-size-calculation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,16 +72,25 @@ public override nfloat GetSizeToFitColumnWidth (NSTableView tableView, nint colu | |
| { | ||
| var tableColumn = Backend.Columns[(int)column] as TableColumn; | ||
| var width = tableColumn.HeaderCell.CellSize.Width; | ||
| var rowWidths = Backend.ColumnRowWidths[(int)column]; | ||
|
|
||
| CompositeCell templateCell = null; | ||
| for (int i = 0; i < tableView.RowCount; i++) { | ||
| if (rowWidths[i] > -1) | ||
| width = (nfloat)Math.Max (width, rowWidths[i]); | ||
| else { | ||
| var cellView = tableView.GetView (column, i, false) as CompositeCell; | ||
| if (cellView == null) { // use template for invisible rows | ||
| cellView = templateCell ?? (templateCell = (tableColumn as TableColumn)?.DataView?.Copy () as CompositeCell); | ||
| if (cellView != null) | ||
| cellView.ObjectValue = NSNumber.FromInt32 (i); | ||
| if (cellView == null) { // use template for invisible rows | ||
| cellView = templateCell ?? (templateCell = (tableColumn as TableColumn)?.DataView as CompositeCell); | ||
| if (cellView != null) | ||
| cellView.ObjectValue = NSNumber.FromInt32 (i); | ||
| } | ||
| if (cellView != null) { | ||
| var size = cellView.FittingSize; | ||
| rowWidths[i] = size.Width; | ||
| width = (nfloat)Math.Max (width, size.Width); | ||
| } | ||
| } | ||
| width = (nfloat)Math.Max (width, cellView.FittingSize.Width); | ||
| } | ||
| return width; | ||
| } | ||
|
|
@@ -95,6 +104,9 @@ public override NSIndexSet GetSelectionIndexes(NSTableView tableView, NSIndexSet | |
| IListDataSource source; | ||
| ListSource tsource; | ||
|
|
||
| List<List<nfloat>> ColumnRowWidths = new List<List<nfloat>> (); | ||
| List<nfloat> RowHeights = new List<nfloat> (); | ||
|
|
||
| protected override NSTableView CreateView () | ||
| { | ||
| var listView = new NSTableViewBackend (this); | ||
|
|
@@ -140,14 +152,36 @@ void HandleDoubleClick (object sender, EventArgs e) | |
| }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public override NSTableColumn AddColumn (ListViewColumn col) | ||
| { | ||
| NSTableColumn tcol = base.AddColumn (col); | ||
| var widths = new List<nfloat> (); | ||
| if (Table.RowCount > 0) | ||
| widths.InsertRange (0, Enumerable.Repeat<nfloat> (-1f, (int)Table.RowCount)); | ||
| ColumnRowWidths.Add (widths); | ||
| return tcol; | ||
| } | ||
|
|
||
| public override void RemoveColumn (ListViewColumn col, object handle) | ||
| { | ||
| var tcol = (NSTableColumn)handle; | ||
| var index = Columns.IndexOf (tcol); | ||
| ColumnRowWidths.RemoveAt (index); | ||
| base.RemoveColumn (col, handle); | ||
| } | ||
|
|
||
| public virtual void SetSource (IListDataSource source, IBackend sourceBackend) | ||
| { | ||
| this.source = source; | ||
|
|
||
| RowHeights = new List<nfloat> (); | ||
| for (int i = 0; i < source.RowCount; i++) | ||
| RowHeights.Add (-1); | ||
| foreach (var colWidths in ColumnRowWidths) { | ||
| colWidths.Clear (); | ||
| colWidths.AddRange (Enumerable.Repeat<nfloat> (-1, source.RowCount)); | ||
|
||
| } | ||
|
|
||
| tsource = new ListSource (source); | ||
| Table.DataSource = tsource; | ||
|
|
@@ -157,30 +191,43 @@ public virtual void SetSource (IListDataSource source, IBackend sourceBackend) | |
| // only the visible rows are reloaded. | ||
| source.RowInserted += (sender, e) => { | ||
| RowHeights.Insert (e.Row, -1); | ||
| foreach (var colWidths in ColumnRowWidths) | ||
| colWidths.Insert (e.Row, -1); | ||
| Table.ReloadData (); | ||
| }; | ||
| source.RowDeleted += (sender, e) => { | ||
| RowHeights.RemoveAt (e.Row); | ||
| foreach (var colWidths in ColumnRowWidths) | ||
| colWidths.RemoveAt (e.Row); | ||
| Table.ReloadData (); | ||
| }; | ||
| source.RowChanged += (sender, e) => { | ||
| UpdateRowHeight (e.Row); | ||
| Table.ReloadData (NSIndexSet.FromIndex (e.Row), NSIndexSet.FromNSRange (new NSRange (0, Table.ColumnCount))); | ||
| }; | ||
| source.RowsReordered += (sender, e) => { RowHeights.Clear (); Table.ReloadData (); }; | ||
| source.RowsReordered += (sender, e) => { | ||
| RowHeights.Clear (); | ||
| foreach (var colWidths in ColumnRowWidths) | ||
| colWidths.Clear (); | ||
| Table.ReloadData (); | ||
| }; | ||
| } | ||
|
|
||
| public override void InvalidateRowHeight (object pos) | ||
| public override void QueueResizeRow (object pos) | ||
| { | ||
| UpdateRowHeight((int)pos); | ||
| } | ||
|
|
||
| List<nfloat> RowHeights = new List<nfloat> (); | ||
|
|
||
| bool updatingRowHeight; | ||
| public void UpdateRowHeight (nint row) | ||
| { | ||
| if (updatingRowHeight) | ||
| return; | ||
|
|
||
| // FIXME: this won't resize the columns, which might be needed for custom cells | ||
| // In order to resize horizontally we'll need trigger column autosizing. | ||
| foreach (var colWidths in ColumnRowWidths) // invalidate widths for full recalculation | ||
| colWidths[(int)row] = -1; | ||
| RowHeights[(int)row] = CalcRowHeight (row); | ||
| Table.NoteHeightOfRowsWithIndexesChanged (NSIndexSet.FromIndex (row)); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will do Insert N times instead of doing a block insert. Avoid direct IEnumerables here. Please pass something
ICollectionderived. (theIEnumerable<T>overload handles checking forICollection<T>)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think pre-allocating list capacity
(new List<nfloat> (table.RowCount)then doingfor (int i = 0; i < Table.RowCount; ++i) widths.Add (-1f)is better