Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: client/samples/total/src/SpreadsheetPresenter.dart

Issue 9148015: Example showing alternate async measurement solution (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Final version Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 typedef void EditCellRangeFunction(CellRange range); 5 typedef void EditCellRangeFunction(CellRange range);
6 6
7 /** 7 /**
8 * Spreadsheet widget. 8 * Spreadsheet widget.
9 */ 9 */
10 class SpreadsheetPresenter implements SpreadsheetListener, SelectionListener { 10 class SpreadsheetPresenter implements SpreadsheetListener, SelectionListener {
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 print("Error parsing formula '${value}': ${exception}"); 657 print("Error parsing formula '${value}': ${exception}");
658 } 658 }
659 } 659 }
660 }); 660 });
661 } 661 }
662 662
663 void _createMoveDragger(Document doc) { 663 void _createMoveDragger(Document doc) {
664 _moveDragger = new Element.tag("div"); 664 _moveDragger = new Element.tag("div");
665 _moveDragger.id = "moveDragger-${_spreadsheet.name}"; 665 _moveDragger.id = "moveDragger-${_spreadsheet.name}";
666 _moveDragger.attributes["class"] = "moveDragger"; 666 _moveDragger.attributes["class"] = "moveDragger";
667 _moveDragger.style.setProperty("left", HtmlUtils.toPx(3)); 667 _moveDragger.style.left = HtmlUtils.toPx(3);
668 _moveDragger.style.setProperty("top", HtmlUtils.toPx(3)); 668 _moveDragger.style.top = HtmlUtils.toPx(3);
669 _spreadsheetElement.nodes.add(_moveDragger); 669 _spreadsheetElement.nodes.add(_moveDragger);
670 670
671 _moveDragger.on.mouseDown.add((MouseEvent e) { 671 _moveDragger.on.mouseDown.add((MouseEvent e) {
672 _moveToTop(); 672 _moveToTop();
673 _hideInnerMenu(true); 673 _hideInnerMenu(true);
674 674
675 int mouseStartX = e.x; 675 int mouseStartX = e.x;
676 int mouseStartY = e.y; 676 int mouseStartY = e.y;
677 677
678 _spreadsheetElement.rect.then((ElementRect elementRect) { 678 window.requestMeasurementFrame(() {
679 ClientRect rect = elementRect.bounding; 679 ClientRect rect = _spreadsheetElement.rect.bounding;
680 int startX = rect.left; 680 int startX = rect.left;
681 int startY = rect.top; 681 int startY = rect.top;
682 _window.document.body.style.setProperty("cursor", "move"); 682 return () {
683 _window.document.body.style.cursor = "move";
683 684
684 _setDragFunction((MouseEvent e_) { 685 _setDragFunction((MouseEvent e_) {
685 int x = startX + e_.x - mouseStartX; 686 int x = startX + e_.x - mouseStartX;
686 int y = startY + e_.y - mouseStartY; 687 int y = startY + e_.y - mouseStartY;
687 688
688 x = Math.max(x, CssStyles.OBJECTBAR_WIDTH); 689 x = Math.max(x, CssStyles.OBJECTBAR_WIDTH);
689 y = Math.max(y, CssStyles.SANDBAR_HEIGHT); 690 y = Math.max(y, CssStyles.SANDBAR_HEIGHT);
690 // Move the spreadsheet container 691 // Move the spreadsheet container
691 _spreadsheetElement.style.setProperty("left", HtmlUtils.toPx(x)); 692 _spreadsheetElement.style.left = HtmlUtils.toPx(x);
692 _spreadsheetElement.style.setProperty("top", HtmlUtils.toPx(y)); 693 _spreadsheetElement.style.top = HtmlUtils.toPx(y);
693 }); 694 });
694 }); 695 };
696 });
695 697
696 _setUndragFunction((MouseEvent e_) { 698 _setUndragFunction((MouseEvent e_) {
697 _window.document.body.style.setProperty("cursor", "auto"); 699 _window.document.body.style.cursor = "auto";
698 }); 700 });
699 }); 701 });
700 } 702 }
701 703
702 void _createResizeDragger(Document doc) { 704 void _createResizeDragger(Document doc) {
703 _resizeDragger = new Element.tag("div"); 705 _resizeDragger = new Element.tag("div");
704 _resizeDragger.id = "resizeDragger-${_spreadsheet.name}"; 706 _resizeDragger.id = "resizeDragger-${_spreadsheet.name}";
705 _resizeDragger.attributes["class"] = "resizeDragger"; 707 _resizeDragger.attributes["class"] = "resizeDragger";
706 _spreadsheetElement.nodes.add(_resizeDragger); 708 _spreadsheetElement.nodes.add(_resizeDragger);
707 709
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 _tableScrollContainer.style.setProperty("left", "0px"); 803 _tableScrollContainer.style.setProperty("left", "0px");
802 _tableScrollContainer.style.setProperty("top", "0px"); 804 _tableScrollContainer.style.setProperty("top", "0px");
803 _tableScrollContainer.nodes.add(_tableScrollDiv); 805 _tableScrollContainer.nodes.add(_tableScrollDiv);
804 _tableSizeChanged(); 806 _tableSizeChanged();
805 807
806 _tableScrollContainer.on.scroll.add((e) { 808 _tableScrollContainer.on.scroll.add((e) {
807 if (_scrolledByKeyboard) { 809 if (_scrolledByKeyboard) {
808 _scrolledByKeyboard = false; 810 _scrolledByKeyboard = false;
809 return; 811 return;
810 } 812 }
811 Future<ElementRect> future = _tableScrollContainer.rect; 813 window.requestMeasurementFrame(() {
812 future.then((ElementRect rect) { 814 final rect = _tableScrollContainer.rect;
813 int scrollTop = rect.scroll.top; 815 int scrollTop = rect.scroll.top;
814 int row = _getAbsRowOrColumn(scrollTop, ROW) - 1; 816 int row = _getAbsRowOrColumn(scrollTop, ROW) - 1;
815 int col = _getAbsRowOrColumn(rect.scroll.left, COL) - 1; 817 int col = _getAbsRowOrColumn(rect.scroll.left, COL) - 1;
816 int newRowShift = Math.max(0, row); 818 int newRowShift = Math.max(0, row);
817 int newColumnShift = Math.max(0, col); 819 int newColumnShift = Math.max(0, col);
818 if (newRowShift != _rowShift || newColumnShift != _columnShift) { 820 if (newRowShift != _rowShift || newColumnShift != _columnShift) {
819 _rowShift = newRowShift; 821 _rowShift = newRowShift;
820 _columnShift = newColumnShift; 822 _columnShift = newColumnShift;
821 _spreadsheet.refresh(); 823 return () { _spreadsheet.refresh(); };
822 } 824 }
823 }); 825 });
824 }); 826 });
825 } 827 }
826 828
827 // Return the number of milliseconds since the epoch 829 // Return the number of milliseconds since the epoch
828 int _currentTimeMllis() { 830 int _currentTimeMllis() {
829 Date now = new Date.now(); 831 Date now = new Date.now();
830 Date then = new Date.fromEpoch(0, now.timeZone); 832 Date then = new Date.fromEpoch(0, now.timeZone);
831 return now.difference(then).inMilliseconds; 833 return now.difference(then).inMilliseconds;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 885
884 // apply text selection now 886 // apply text selection now
885 _formulaCellSelectingSelectText(); 887 _formulaCellSelectingSelectText();
886 888
887 // show cell selecting div 889 // show cell selecting div
888 { 890 {
889 DivElement div = _table.formulaCellSelectingDiv; 891 DivElement div = _table.formulaCellSelectingDiv;
890 CSSStyleDeclaration divStyle = div.style; 892 CSSStyleDeclaration divStyle = div.style;
891 int borderWidth = 2 + 2; 893 int borderWidth = 2 + 2;
892 CellRange cellRange = new CellRange(_spreadsheet, minCorner, maxCorner); 894 CellRange cellRange = new CellRange(_spreadsheet, minCorner, maxCorner);
893 _selectionManager.getBoundingBoxForRange(cellRange).then( 895 window.requestMeasurementFrame(() {
894 (BoundingBox box) { 896 final box = _selectionManager.getBoundingBoxForRange(cellRange);
895 if (box != null) { 897 return () {
896 divStyle.setProperty("left", HtmlUtils.toPx(box.left)); 898 if (box != null) {
897 divStyle.setProperty("top", HtmlUtils.toPx(box.top)); 899 divStyle.left = HtmlUtils.toPx(box.left);
898 divStyle.setProperty("width", HtmlUtils.toPx(box.width - borderWidth)) ; 900 divStyle.top = HtmlUtils.toPx(box.top);
899 divStyle.setProperty("height", HtmlUtils.toPx(box.height - borderWidth )); 901 divStyle.width = HtmlUtils.toPx(box.width - borderWidth);
900 divStyle.removeProperty("display"); 902 divStyle.height = HtmlUtils.toPx(box.height - borderWidth);
901 } else { 903 divStyle.removeProperty("display");
902 divStyle.setProperty("display", "none"); 904 } else {
903 } 905 divStyle.setProperty("display", "none");
906 }
907 };
904 }); 908 });
905 } 909 }
906 } 910 }
907 911
908 void _formulaCellSelectingRememberSelectionRange() { 912 void _formulaCellSelectingRememberSelectionRange() {
909 _formulaCellSelectingTextStart = _formulaInput.selectionStart; 913 _formulaCellSelectingTextStart = _formulaInput.selectionStart;
910 _formulaCellSelectingTextEnd = _formulaInput.selectionEnd; 914 _formulaCellSelectingTextEnd = _formulaInput.selectionEnd;
911 } 915 }
912 916
913 void _formulaCellSelectingSelectText() { 917 void _formulaCellSelectingSelectText() {
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 1074
1071 // Return the number of _rows currently being displayed (not counting the colu mn header row) 1075 // Return the number of _rows currently being displayed (not counting the colu mn header row)
1072 int _getVisibleTableHeight() => _getShiftedRowEnd(_rowShift + _rows, _rowShift ); 1076 int _getVisibleTableHeight() => _getShiftedRowEnd(_rowShift + _rows, _rowShift );
1073 1077
1074 // Return the number of _columns currently being displayed (not counting the r ow header column) 1078 // Return the number of _columns currently being displayed (not counting the r ow header column)
1075 int _getVisibleTableWidth() => _getShiftedColumnEnd(_columnShift + _columns, _ columnShift); 1079 int _getVisibleTableWidth() => _getShiftedColumnEnd(_columnShift + _columns, _ columnShift);
1076 1080
1077 // Resize the formula input field to fit the contained text 1081 // Resize the formula input field to fit the contained text
1078 void _growFormulaInput() { 1082 void _growFormulaInput() {
1079 _formulaInputMeasure.text = _formulaInput.value; 1083 _formulaInputMeasure.text = _formulaInput.value;
1080 _formulaInputMeasure.rect.then((ElementRect rect) { 1084 window.requestMeasurementFrame(() {
1081 int textWidth = rect.client.width; 1085 int textWidth = _formulaInputMeasure.rect.client.width;
1082 int width = Math.max(textWidth + 25, _formulaCellWidth); 1086 int width = Math.max(textWidth + 25, _formulaCellWidth);
1083 _formulaDiv.style.setProperty("width", HtmlUtils.toPx(width)); 1087 return () {
1084 _formulaInput.style.setProperty("width", HtmlUtils.toPx(width)); 1088 _formulaDiv.style.width = HtmlUtils.toPx(width);
1089 _formulaInput.style.width = HtmlUtils.toPx(width);
1090 };
1085 }); 1091 });
1086 } 1092 }
1087 1093
1088 // Fade out the formula input field 1094 // Fade out the formula input field
1089 void _hideFormula() { 1095 void _hideFormula() {
1090 _hideFormulaCellSelecting(); 1096 _hideFormulaCellSelecting();
1091 _formulaEditing = false; 1097 _formulaEditing = false;
1092 _formulaDiv.classes.remove("fadeIn"); 1098 _formulaDiv.classes.remove("fadeIn");
1093 _formulaDiv.classes.add("fadeOut"); 1099 _formulaDiv.classes.add("fadeOut");
1094 } 1100 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1143 // update table row and column headers 1149 // update table row and column headers
1144 void _redrawHeaders() { 1150 void _redrawHeaders() {
1145 _setTableWidth(_getVisibleTableWidth()); 1151 _setTableWidth(_getVisibleTableWidth());
1146 _tableSizeChanged(); 1152 _tableSizeChanged();
1147 1153
1148 _table.redrawHeaders(_selectionManager, _rows, _columns, _rowShift, _columnS hift, 1154 _table.redrawHeaders(_selectionManager, _rows, _columns, _rowShift, _columnS hift,
1149 _cellDisplay); 1155 _cellDisplay);
1150 } 1156 }
1151 1157
1152 void _refreshResizeDragger() { 1158 void _refreshResizeDragger() {
1153 _table.rect.then((ElementRect elementRect) { 1159 window.requestMeasurementFrame(() {
1154 // We may be called before the dragger is ready 1160 // We may be called before the dragger is ready
1155 if (_resizeDragger == null) { 1161 if (_resizeDragger == null) {
1156 return; 1162 return;
1157 } 1163 }
1158 ClientRect rect = elementRect.bounding; 1164 ClientRect rect = _table.rect.bounding;
1159 1165 return () {
1160 _resizeDragger.style.setProperty("left", HtmlUtils.toPx(rect.width)); 1166 _resizeDragger.style.setProperty("left", HtmlUtils.toPx(rect.width));
1161 _resizeDragger.style.setProperty("top", HtmlUtils.toPx(rect.height)); 1167 _resizeDragger.style.setProperty("top", HtmlUtils.toPx(rect.height));
1168 };
1162 }); 1169 });
1163 } 1170 }
1164 1171
1165 // Remove the HTML elements corresponding to the given column 1172 // Remove the HTML elements corresponding to the given column
1166 void _removeTableColumnHtml(int col) { 1173 void _removeTableColumnHtml(int col) {
1167 _table.removeColumn(col); 1174 _table.removeColumn(col);
1168 int width = _spreadsheet.getColumnWidth(col); 1175 int width = _spreadsheet.getColumnWidth(col);
1169 _setTableWidth(_tableWidth - width); 1176 _setTableWidth(_tableWidth - width);
1170 } 1177 }
1171 1178
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 setDrag(COL, false); 1470 setDrag(COL, false);
1464 } 1471 }
1465 }); 1472 });
1466 1473
1467 // The inline cell editing UI is displayed when a user makes the same single 1474 // The inline cell editing UI is displayed when a user makes the same single
1468 // cell selection over again. We keep track of the current single cell that 1475 // cell selection over again. We keep track of the current single cell that
1469 // is selected here to know when to enter edit mode. 1476 // is selected here to know when to enter edit mode.
1470 CellLocation currentSelectedSingleCell = null; 1477 CellLocation currentSelectedSingleCell = null;
1471 1478
1472 void mouseMove(MouseEvent e) { 1479 void mouseMove(MouseEvent e) {
1473 _table.rect.then((ElementRect rect) { 1480 window.requestMeasurementFrame(() {
1474 // Set x and y to the mouse coordinates, relative to the top left of 1481 // Set x and y to the mouse coordinates, relative to the top left of
1475 // the spreadsheet table. 1482 // the spreadsheet table.
1476 ClientRect boundingRect = rect.bounding; 1483 ClientRect boundingRect = _table.rect.bounding;
1477 int scrollOffsetX = -boundingRect.left.toInt(); 1484 int scrollOffsetX = -boundingRect.left.toInt();
1478 int scrollOffsetY = -boundingRect.top.toInt(); 1485 int scrollOffsetY = -boundingRect.top.toInt();
1479 x = e.x + scrollOffsetX; 1486 x = e.x + scrollOffsetX;
1480 y = e.y + scrollOffsetY; 1487 y = e.y + scrollOffsetY;
1481 1488
1482 // Update the dragger position and optionally the actual row/column size 1489 // Update the dragger position and optionally the actual row/column size
1483 if (dragRowColumn(false)) { 1490 if (dragRowColumn(false)) {
1484 return; 1491 return;
1485 } 1492 }
1486 1493
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1535 }); 1542 });
1536 } 1543 }
1537 1544
1538 _setMove(mouseMove); 1545 _setMove(mouseMove);
1539 1546
1540 _table.on.mouseDown.add((MouseEvent e) { 1547 _table.on.mouseDown.add((MouseEvent e) {
1541 _moveToTop(); 1548 _moveToTop();
1542 1549
1543 // Right click toggles and positions the context menu 1550 // Right click toggles and positions the context menu
1544 if (e.button == 2 || (e.button == 0 && e.ctrlKey)) { 1551 if (e.button == 2 || (e.button == 0 && e.ctrlKey)) {
1545 _table.rect.then((ElementRect rect) { 1552 window.requestMeasurementFrame(() {
1546 ClientRect boundingRect = rect.bounding; 1553 ClientRect boundingRect = _table.rect.bounding;
1547 int scrollOffsetX = -boundingRect.left; 1554 return () {
1548 int scrollOffsetY = -boundingRect.top; 1555 int scrollOffsetX = -boundingRect.left;
1549 _contextMenu.show(e.x + scrollOffsetX, e.y + scrollOffsetY); 1556 int scrollOffsetY = -boundingRect.top;
1557 _contextMenu.show(e.x + scrollOffsetX, e.y + scrollOffsetY);
1558 };
1550 }); 1559 });
1551 return; 1560 return;
1552 } 1561 }
1553 1562
1554 _setDragFunction(mouseMove); 1563 _setDragFunction(mouseMove);
1555 1564
1556 _setUndragFunction((MouseEvent e_) { 1565 _setUndragFunction((MouseEvent e_) {
1557 dragRowColumn(true); 1566 dragRowColumn(true);
1558 _table.setDefaultCursor(); 1567 _table.setDefaultCursor();
1559 draggingSelection = false; 1568 draggingSelection = false;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1681 } 1690 }
1682 _activeInnerMenu = new InnerMenuView(_window, row, _selectionManager, style, 1691 _activeInnerMenu = new InnerMenuView(_window, row, _selectionManager, style,
1683 _spreadsheet.layout.getRowHeight(rowIndex), 1692 _spreadsheet.layout.getRowHeight(rowIndex),
1684 (){ _tableSizeChanged(); }, 1693 (){ _tableSizeChanged(); },
1685 (){ _hideInnerMenu(true); _repositionFormulaInput(null, false); }); 1694 (){ _hideInnerMenu(true); _repositionFormulaInput(null, false); });
1686 _innerMenuRowIndex = rowIndex; 1695 _innerMenuRowIndex = rowIndex;
1687 } 1696 }
1688 1697
1689 // Update the scroll mechanism due to a change in the visible table area 1698 // Update the scroll mechanism due to a change in the visible table area
1690 void _tableSizeChanged() { 1699 void _tableSizeChanged() {
1691 _table.rect.then((ElementRect elementRect) { 1700 window.requestMeasurementFrame(() {
1692 ClientRect rect = elementRect.bounding; 1701 ClientRect rect = _table.rect.bounding;
1702 return () {
1703 _tableScrollContainer.style.width = HtmlUtils.toPx(rect.width + 10);
1704 _spreadsheetElement.style.width = HtmlUtils.toPx(rect.width);
1693 1705
1694 _tableScrollContainer.style.width = HtmlUtils.toPx(rect.width + 10); 1706 _tableScrollContainer.style.height = HtmlUtils.toPx(rect.height + 10);
1695 _spreadsheetElement.style.width = HtmlUtils.toPx(rect.width); 1707 _spreadsheetElement.style.height = HtmlUtils.toPx(rect.height);
1696 1708
1697 _tableScrollContainer.style.height = HtmlUtils.toPx(rect.height + 10); 1709 _tableScrollDiv.style.width = HtmlUtils.toPx(
1698 _spreadsheetElement.style.height = HtmlUtils.toPx(rect.height); 1710 _spreadsheet.getColumnEnd(_spreadsheet.columnCount()));
1711 int extra = _activeInnerMenu == null ?
1712 0 : _activeInnerMenu.currentRowHeight;
1713 _tableScrollDiv.style.height = HtmlUtils.toPx(_spreadsheet.getRowEnd(
1714 _spreadsheet.rowCount()) + extra);
1699 1715
1700 _tableScrollDiv.style.width = HtmlUtils.toPx( 1716 // Reposition the scroll bars
1701 _spreadsheet.getColumnEnd(_spreadsheet.columnCount())); 1717 _scroll(_rowShift, _columnShift);
1702 int extra = _activeInnerMenu == null ? 1718 // Move the resize dragger to the bottom-right corner
1703 0 : _activeInnerMenu.currentRowHeight; 1719 _refreshResizeDragger();
1704 _tableScrollDiv.style.height = HtmlUtils.toPx(_spreadsheet.getRowEnd( 1720 };
1705 _spreadsheet.rowCount()) + extra);
1706
1707 // Reposition the scroll bars
1708 _scroll(_rowShift, _columnShift);
1709 // Move the resize dragger to the bottom-right corner
1710 _refreshResizeDragger();
1711 }); 1721 });
1712 } 1722 }
1713 1723
1714 void _updateInnerMenu() { 1724 void _updateInnerMenu() {
1715 if (_activeInnerMenu != null) { 1725 if (_activeInnerMenu != null) {
1716 _activeInnerMenu.updateSize(); 1726 _activeInnerMenu.updateSize();
1717 } 1727 }
1718 } 1728 }
1719 1729
1720 // Change the width/height of a column or row. 1730 // Change the width/height of a column or row.
1721 void _updateRowColumnSize(int rowOrCol, int index, int size, int oldSize, bool mouseUp) { 1731 void _updateRowColumnSize(int rowOrCol, int index, int size, int oldSize, bool mouseUp) {
1722 if (mouseUp) { 1732 if (mouseUp) {
1723 Command command = new ResizeRowColumnCommand(_spreadsheet, rowOrCol, index , size, oldSize); 1733 Command command = new ResizeRowColumnCommand(_spreadsheet, rowOrCol, index , size, oldSize);
1724 _spreadsheet.execute(command); 1734 _spreadsheet.execute(command);
1725 } else { 1735 } else {
1726 if (rowOrCol == COL) { 1736 if (rowOrCol == COL) {
1727 _spreadsheet.setColumnWidth(index, size); 1737 _spreadsheet.setColumnWidth(index, size);
1728 } else { 1738 } else {
1729 _spreadsheet.setRowHeight(index, size); 1739 _spreadsheet.setRowHeight(index, size);
1730 } 1740 }
1731 } 1741 }
1732 } 1742 }
1733 } 1743 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698