mdiwindow.as
来自「flex的一些小例子」· AS 代码 · 共 1,764 行 · 第 1/4 页
AS
1,764 行
// if the panel is floating, save its state if(windowState == MDIWindowState.NORMAL) { savePanel(); } dispatchEvent(new MDIWindowEvent(MDIWindowEvent.MINIMIZE, this)); windowState = MDIWindowState.MINIMIZED; showControls = false; } /** * Called from maximize/restore button * * @event MouseEvent (optional) */ public function maximizeRestore(event:MouseEvent = null):void { if(windowState == MDIWindowState.NORMAL) { savePanel(); maximize(); } else { restore(); } } /** * Restores the window to its last floating position. */ public function restore():void { windowState = MDIWindowState.NORMAL; updateStyles(); dispatchEvent(new MDIWindowEvent(MDIWindowEvent.RESTORE, this)); } /** * Maximize the window. */ public function maximize():void { if(windowState == MDIWindowState.NORMAL) { savePanel(); } showControls = true; windowState = MDIWindowState.MAXIMIZED; updateStyles(); dispatchEvent(new MDIWindowEvent(MDIWindowEvent.MAXIMIZE, this)); } /** * Close the window. */ public function close(event:MouseEvent = null):void { dispatchEvent(new MDIWindowEvent(MDIWindowEvent.CLOSE, this)); } /** * Save the panel's floating coordinates. * * @private */ private function savePanel():void { savedWindowRect = new Rectangle(this.x, this.y, this.width, this.height); } /** * Title bar dragging. * * @private */ private function onTitleBarPress(event:MouseEvent):void { // only floating windows can be dragged if(this.windowState == MDIWindowState.NORMAL && draggable) { if(windowManager.enforceBoundaries) { this.startDrag(false, new Rectangle(0, 0, parent.width - this.width, parent.height - this.height)); } else { this.startDrag(); } systemManager.addEventListener(MouseEvent.MOUSE_MOVE, onWindowMove); systemManager.addEventListener(MouseEvent.MOUSE_UP, onTitleBarRelease); systemManager.stage.addEventListener(Event.MOUSE_LEAVE, onTitleBarRelease); } } private function onWindowMove(event:MouseEvent):void { if(!_dragging) { _dragging = true; // clear styles (future versions may allow enforcing constraints on drag) this.clearStyle("top"); this.clearStyle("right"); this.clearStyle("bottom"); this.clearStyle("left"); dispatchEvent(new MDIWindowEvent(MDIWindowEvent.DRAG_START, this)); } dispatchEvent(new MDIWindowEvent(MDIWindowEvent.DRAG, this)); } private function onTitleBarRelease(event:Event):void { this.stopDrag(); if(_dragging) { _dragging = false; dispatchEvent(new MDIWindowEvent(MDIWindowEvent.DRAG_END, this)); } systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, onWindowMove); systemManager.removeEventListener(MouseEvent.MOUSE_UP, onTitleBarRelease); systemManager.stage.removeEventListener(Event.MOUSE_LEAVE, onTitleBarRelease); } /** * Mouse down on any resize handle. */ private function onResizeButtonPress(event:MouseEvent):void { if(windowState == MDIWindowState.NORMAL && resizable) { currentResizeHandle = event.target as Button; setCursor(currentResizeHandle); dragStartMouseX = parent.mouseX; dragStartMouseY = parent.mouseY; savePanel(); dragMaxX = savedWindowRect.x + (savedWindowRect.width - minWidth); dragMaxY = savedWindowRect.y + (savedWindowRect.height - minHeight); systemManager.addEventListener(Event.ENTER_FRAME, updateWindowSize, false, 0, true); systemManager.addEventListener(MouseEvent.MOUSE_MOVE, onResizeButtonDrag, false, 0, true); systemManager.addEventListener(MouseEvent.MOUSE_UP, onResizeButtonRelease, false, 0, true); systemManager.stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeaveStage, false, 0, true); } } private function onResizeButtonDrag(event:MouseEvent):void { if(!_resizing) { _resizing = true; dispatchEvent(new MDIWindowEvent(MDIWindowEvent.RESIZE_START, this)); } dispatchEvent(new MDIWindowEvent(MDIWindowEvent.RESIZE, this)); } /** * Mouse move while mouse is down on a resize handle */ private function updateWindowSize(event:Event):void { if(windowState == MDIWindowState.NORMAL && resizable) { dragAmountX = parent.mouseX - dragStartMouseX; dragAmountY = parent.mouseY - dragStartMouseY; if(currentResizeHandle == resizeHandleTop && parent.mouseY > 0) { this.y = Math.min(savedWindowRect.y + dragAmountY, dragMaxY); this.height = Math.max(savedWindowRect.height - dragAmountY, minHeight); } else if(currentResizeHandle == resizeHandleRight && parent.mouseX < parent.width) { this.width = Math.max(savedWindowRect.width + dragAmountX, minWidth); } else if(currentResizeHandle == resizeHandleBottom && parent.mouseY < parent.height) { this.height = Math.max(savedWindowRect.height + dragAmountY, minHeight); } else if(currentResizeHandle == resizeHandleLeft && parent.mouseX > 0) { this.x = Math.min(savedWindowRect.x + dragAmountX, dragMaxX); this.width = Math.max(savedWindowRect.width - dragAmountX, minWidth); } else if(currentResizeHandle == resizeHandleTL && parent.mouseX > 0 && parent.mouseY > 0) { this.x = Math.min(savedWindowRect.x + dragAmountX, dragMaxX); this.y = Math.min(savedWindowRect.y + dragAmountY, dragMaxY); this.width = Math.max(savedWindowRect.width - dragAmountX, minWidth); this.height = Math.max(savedWindowRect.height - dragAmountY, minHeight); } else if(currentResizeHandle == resizeHandleTR && parent.mouseX < parent.width && parent.mouseY > 0) { this.y = Math.min(savedWindowRect.y + dragAmountY, dragMaxY); this.width = Math.max(savedWindowRect.width + dragAmountX, minWidth); this.height = Math.max(savedWindowRect.height - dragAmountY, minHeight); } else if(currentResizeHandle == resizeHandleBR && parent.mouseX < parent.width && parent.mouseY < parent.height) { this.width = Math.max(savedWindowRect.width + dragAmountX, minWidth); this.height = Math.max(savedWindowRect.height + dragAmountY, minHeight); } else if(currentResizeHandle == resizeHandleBL && parent.mouseX > 0 && parent.mouseY < parent.height) { this.x = Math.min(savedWindowRect.x + dragAmountX, dragMaxX); this.width = Math.max(savedWindowRect.width - dragAmountX, minWidth); this.height = Math.max(savedWindowRect.height + dragAmountY, minHeight); } } } private function onResizeButtonRelease(event:MouseEvent = null):void { if(windowState == MDIWindowState.NORMAL && resizable) { if(_resizing) { _resizing = false; dispatchEvent(new MDIWindowEvent(MDIWindowEvent.RESIZE_END, this)); } currentResizeHandle = null; systemManager.removeEventListener(Event.ENTER_FRAME, updateWindowSize); systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, onResizeButtonDrag); systemManager.removeEventListener(MouseEvent.MOUSE_UP, onResizeButtonRelease); systemManager.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveStage); CursorManager.removeCursor(CursorManager.currentCursorID); } } private function onMouseLeaveStage(event:Event):void { onResizeButtonRelease(); systemManager.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveStage); } /** * Restore window to state it was in prior to being minimized. */ public function unMinimize(event:MouseEvent = null):void { if(minimized) { showControls = true; if(_prevWindowState == MDIWindowState.NORMAL) { restore(); } else { maximize(); } } } private function setCursor(target:Button):void { var styleStub:String; switch(target) { case resizeHandleRight: case resizeHandleLeft: styleStub = "resizeCursorHorizontal"; break; case resizeHandleTop: case resizeHandleBottom: styleStub = "resizeCursorVertical"; break; case resizeHandleTL: case resizeHandleBR: styleStub = "resizeCursorTopLeftBottomRight"; break; case resizeHandleTR: case resizeHandleBL: styleStub = "resizeCursorTopRightBottomLeft"; break; } var selectorList:Array = getSelectorList(); CursorManager.removeCursor(CursorManager.currentCursorID); CursorManager.setCursor(Class(getStyleByPriority(selectorList, styleStub + "Skin")), 2, Number(getStyleByPriority(selectorList, styleStub + "XOffset")), Number(getStyleByPriority(selectorList, styleStub + "YOffset"))); } private function onResizeButtonRollOver(event:MouseEvent):void { // only floating windows can be resized // event.buttonDown is to detect being dragged over if(windowState == MDIWindowState.NORMAL && resizable && !event.buttonDown) { setCursor(event.target as Button); } } private function onResizeButtonRollOut(event:MouseEvent):void { if(!event.buttonDown) { CursorManager.removeCursor(CursorManager.currentCursorID); } } public function set showControls(value:Boolean):void { Container(windowControls).visible = value; } private function get windowState():int { return _windowState; } private function set windowState(newState:int):void { _prevWindowState = _windowState; _windowState = newState; updateContextMenu(); } public function get minimized():Boolean { return _windowState == MDIWindowState.MINIMIZED; } public function get maximized():Boolean { return _windowState == MDIWindowState.MAXIMIZED; } public function get minimizeHeight():Number { return titleBar.height; } public static const CONTEXT_MENU_LABEL_MINIMIZE:String = "Minimize"; public static const CONTEXT_MENU_LABEL_MAXIMIZE:String = "Maximize"; public static const CONTEXT_MENU_LABEL_RESTORE:String = "Restore"; public static const CONTEXT_MENU_LABEL_CLOSE:String = "Close"; public function updateContextMenu():void { var defaultContextMenu:ContextMenu = new ContextMenu(); defaultContextMenu.hideBuiltInItems(); var minimizeItem:ContextMenuItem = new ContextMenuItem(MDIWindow.CONTEXT_MENU_LABEL_MINIMIZE); minimizeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); minimizeItem.enabled = windowState != MDIWindowState.MINIMIZED; defaultContextMenu.customItems.push(minimizeItem); var maximizeItem:ContextMenuItem = new ContextMenuItem(MDIWindow.CONTEXT_MENU_LABEL_MAXIMIZE); maximizeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); maximizeItem.enabled = windowState != MDIWindowState.MAXIMIZED; defaultContextMenu.customItems.push(maximizeItem); var restoreItem:ContextMenuItem = new ContextMenuItem(MDIWindow.CONTEXT_MENU_LABEL_RESTORE); restoreItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); restoreItem.enabled = windowState != MDIWindowState.NORMAL; defaultContextMenu.customItems.push(restoreItem); var closeItem:ContextMenuItem = new ContextMenuItem(MDIWindow.CONTEXT_MENU_LABEL_CLOSE); closeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(closeItem); var arrangeItem:ContextMenuItem = new ContextMenuItem(MDIManager.CONTEXT_MENU_LABEL_TILE); arrangeItem.separatorBefore = true; arrangeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(arrangeItem); var arrangeFillItem:ContextMenuItem = new ContextMenuItem(MDIManager.CONTEXT_MENU_LABEL_TILE_FILL); arrangeFillItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(arrangeFillItem); var cascadeItem:ContextMenuItem = new ContextMenuItem(MDIManager.CONTEXT_MENU_LABEL_CASCADE); cascadeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(cascadeItem); var showAllItem:ContextMenuItem = new ContextMenuItem(MDIManager.CONTEXT_MENU_LABEL_SHOW_ALL); showAllItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(showAllItem); this.contextMenu = defaultContextMenu; } private function menuItemSelectHandler(event:ContextMenuEvent):void { switch(event.target.caption) { case(MDIWindow.CONTEXT_MENU_LABEL_MINIMIZE): minimize(); break; case(MDIWindow.CONTEXT_MENU_LABEL_MAXIMIZE): maximize(); break; case(MDIWindow.CONTEXT_MENU_LABEL_RESTORE): if(this.windowState == MDIWindowState.MINIMIZED) { unMinimize(); } else if(this.windowState == MDIWindowState.MAXIMIZED) { maximizeRestore(); } break; case(MDIWindow.CONTEXT_MENU_LABEL_CLOSE): close(); break; case(MDIManager.CONTEXT_MENU_LABEL_TILE): this.windowManager.tile(false, this.windowManager.tilePadding); break; case(MDIManager.CONTEXT_MENU_LABEL_TILE_FILL): this.windowManager.tile(true, this.windowManager.tilePadding); break; case(MDIManager.CONTEXT_MENU_LABEL_CASCADE): this.windowManager.cascade(); break; case(MDIManager.CONTEXT_MENU_LABEL_SHOW_ALL): this.windowManager.showAllWindows(); break; } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?