Annotation of modules/damieng/graphical_editor/daxe/lib/src/menubar.dart, revision 1.1
1.1 ! damieng 1: /*
! 2: This file is part of Daxe.
! 3:
! 4: Daxe is free software: you can redistribute it and/or modify
! 5: it under the terms of the GNU General Public License as published by
! 6: the Free Software Foundation, either version 3 of the License, or
! 7: (at your option) any later version.
! 8:
! 9: Daxe is distributed in the hope that it will be useful,
! 10: but WITHOUT ANY WARRANTY; without even the implied warranty of
! 11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 12: GNU General Public License for more details.
! 13:
! 14: You should have received a copy of the GNU General Public License
! 15: along with Daxe. If not, see <http://www.gnu.org/licenses/>.
! 16: */
! 17:
! 18: part of daxe;
! 19:
! 20: class MenuBar {
! 21: List<Menu> menus;
! 22: bool ignoreClick;
! 23: Menu visibleMenu;
! 24:
! 25: MenuBar() {
! 26: menus = new List<Menu>();
! 27: ignoreClick = false;
! 28: visibleMenu = null;
! 29: }
! 30:
! 31: add(Menu m) {
! 32: menus.add(m);
! 33: }
! 34:
! 35: insert(Menu m, int position) {
! 36: menus.insert(position, m);
! 37: }
! 38:
! 39: h.Element html() {
! 40: h.DivElement div = new h.DivElement();
! 41: div.classes.add('menubar');
! 42: bool addedTabindex = false;
! 43: for (Menu m in menus) {
! 44: h.DivElement divMenu = createMenuDiv(m);
! 45: div.append(divMenu);
! 46: if (!addedTabindex && m.enabled) {
! 47: divMenu.setAttribute('tabindex', '0');
! 48: addedTabindex = true;
! 49: }
! 50: }
! 51: h.document.onMouseUp.listen((h.MouseEvent event) => docMouseUp(event));
! 52: return(div);
! 53: }
! 54:
! 55: h.DivElement createMenuDiv(Menu m) {
! 56: h.DivElement divMenu = new h.DivElement();
! 57: divMenu.text = m.title;
! 58: divMenu.id = m.itemid;
! 59: divMenu.classes.add('menu_title');
! 60: if (m.parent is Toolbar)
! 61: divMenu.setAttribute('tabindex', '0');
! 62: else
! 63: divMenu.setAttribute('tabindex', '-1');
! 64: divMenu.onMouseDown.listen((h.MouseEvent event) => mouseDown(event, m));
! 65: divMenu.onMouseOver.listen((h.MouseEvent event) => mouseOver(event, m));
! 66: divMenu.onClick.listen((h.MouseEvent event) => click(m));
! 67: divMenu.onKeyDown.listen((h.KeyboardEvent event) {
! 68: if (h.document.activeElement != divMenu)
! 69: return; // an item could have the focus, and the onKeyDown will trigger
! 70: int keyCode = event.keyCode;
! 71: if (keyCode == h.KeyCode.ENTER || keyCode == h.KeyCode.DOWN) {
! 72: event.preventDefault();
! 73: if (m == visibleMenu)
! 74: m.selectFirst();
! 75: else
! 76: showMenu(m);
! 77: } else if (keyCode == h.KeyCode.LEFT) {
! 78: selectPrevious(m);
! 79: } else if (keyCode == h.KeyCode.RIGHT) {
! 80: selectNext(m);
! 81: } else if (keyCode == h.KeyCode.TAB) {
! 82: Timer.run(hideVisibleMenu);
! 83: }
! 84: });
! 85: h.Element dropdown = m.htmlMenu();
! 86: dropdown.style.display = 'none';
! 87: divMenu.append(dropdown);
! 88: return(divMenu);
! 89: }
! 90:
! 91: void mouseDown(h.MouseEvent event, Menu m) {
! 92: event.preventDefault();
! 93: if (!m.isVisible()) {
! 94: showMenu(m);
! 95: ignoreClick = true;
! 96: } else {
! 97: ignoreClick = false;
! 98: }
! 99: }
! 100:
! 101: void mouseOver(h.MouseEvent event, Menu m) {
! 102: if (visibleMenu == null || visibleMenu == m)
! 103: return;
! 104: hideMenu(visibleMenu);
! 105: showMenu(m);
! 106: }
! 107:
! 108: void click(Menu m) {
! 109: if (ignoreClick) {
! 110: return;
! 111: }
! 112: if (!m.isVisible()) {
! 113: showMenu(m);
! 114: } else {
! 115: hideMenu(m);
! 116: }
! 117: }
! 118:
! 119: void docMouseUp(h.MouseEvent event) {
! 120: if (visibleMenu == null)
! 121: return;
! 122: h.DivElement divMenu = h.querySelector("#${visibleMenu.itemid}");
! 123: h.Rectangle r = divMenu.getBoundingClientRect();
! 124: if (event.client.x < r.left || event.client.x > r.right ||
! 125: event.client.y < r.top || event.client.y > r.bottom) {
! 126: hideMenu(visibleMenu);
! 127: ignoreClick = true;
! 128: }
! 129: }
! 130:
! 131: void showMenu(Menu m) {
! 132: visibleMenu = m;
! 133: h.DivElement divMenu = h.querySelector("#${m.itemid}");
! 134: divMenu.classes.add('selected');
! 135: m.show();
! 136: m.getItemHTMLNode().focus();
! 137: }
! 138:
! 139: void hideMenu(Menu m) {
! 140: visibleMenu = null;
! 141: h.DivElement divMenu = h.querySelector("#${m.itemid}");
! 142: divMenu.classes.remove('selected');
! 143: m.hide();
! 144: }
! 145:
! 146: void hideVisibleMenu() {
! 147: if (visibleMenu != null)
! 148: hideMenu(visibleMenu);
! 149: }
! 150:
! 151: void selectPrevious(Menu current) {
! 152: if (current == null)
! 153: current = visibleMenu;
! 154: if (current == null)
! 155: return;
! 156: bool found = false;
! 157: for (Menu m in menus.reversed) {
! 158: if (m == current) {
! 159: found = true;
! 160: } else if (found && m.enabled) {
! 161: hideMenu(current);
! 162: showMenu(m);
! 163: return;
! 164: }
! 165: }
! 166: }
! 167:
! 168: void selectNext(Menu current) {
! 169: if (current == null)
! 170: current = visibleMenu;
! 171: if (current == null)
! 172: return;
! 173: bool found = false;
! 174: for (Menu m in menus) {
! 175: if (m == current) {
! 176: found = true;
! 177: } else if (found && m.enabled) {
! 178: hideMenu(current);
! 179: showMenu(m);
! 180: return;
! 181: }
! 182: }
! 183: }
! 184:
! 185: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>