//minmax 限制窗口大小
import win.appBar;
namespace win.ui;

class minmax{ 
	ctor(winform,minCx,minCy,maxCx,maxCy){  
		if( ! (minCx && minCy) ){
			var rc = ..win.getPlacement(winform.hwnd).rcNormalPosition;
			minCx = rc.width();
			minCy = rc.height(); 
		}
		
		this.dpiScaleX = winform.dpiScaleX : 1;
		this.dpiScaleY = winform.dpiScaleY : 1;
		
		this.updateMinMaxInfo = function(){
			var x,y,cx,cy = 0,0,maxCx,maxCy;
			if(winform.dpiScaleX && winform.dpiScaleY){
				if(maxCx!==null) cx = maxCx / this.dpiScaleX * winform.dpiScaleX;
				if(maxCy!==null) cy = maxCy / this.dpiScaleY * winform.dpiScaleY;	
			} 
			
			if(..win.getStyle(winform.hwnd,0x800000/*_WS_BORDER*/)) { 
				if(cx!==null) this.maxTrackSize.x = cx; 
				if(cy!==null) this.maxTrackSize.y = cy;
				return;
			}
			
			var maxX,maxY = 0,0;
			var monitorInfo = ..win.getMonitorInfo(winform.hwnd);
			
			if( monitorInfo){
				if(!cx) cx = monitorInfo.rcWork.right - monitorInfo.rcWork.left; 
				if(!cy) cy = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
				x = monitorInfo.rcWork.left - monitorInfo.rcMonitor.left;
				y = monitorInfo.rcWork.top - monitorInfo.rcMonitor.top; 
			}
			else{
				var rcWork = ..win.getWorkArea();
				if(!cx) cx = rcWork.width();
				if(!cy) cy = rcWork.height(); 
      			x = rcWork.left;
      			y = rcWork.top; 
      		};

      		var taskBar =  ..win.appBar.getTaskBar();
      		if( taskBar ? taskBar.state == 1/*_ABS_AUTOHIDE*/){
      			var edgeY = ::GetSystemMetrics(46/*_SM_CYEDGE*/);
      			var edgeX = ::GetSystemMetrics(46/*_SM_CYEDGE*/);
      		
      			if( taskBar.uEdge == 0x3/*_ABE_BOTTOM*/ ){
					cy -= edgeY;
				}
				elseif( taskBar.uEdge == 0x2/*_ABE_RIGHT*/ ){
					cx -= edgeX;
				}
				elseif( taskBar.uEdge == 0x1/*_ABE_TOP*/ ){
					y += edgeY;
					cy -= edgeY;
				}
				elseif( taskBar.uEdge == 0x0/*_ABE_LEFT*/ ){
					x += edgeX;
					cx -= edgeX;
				}
			}
			
			if( this.minTrackSize.x >  cx ){
				this.minTrackSize.x = cx;
			}
			if( this.minTrackSize.y >  cy ){
				this.minTrackSize.y = cy;
			}
			
      		this.maxSize.x = cx;
      		this.maxSize.y = cy;
      		this.maxTrackSize.x = cx;
      		this.maxTrackSize.y = cy;
      		this.maxPosition.x = x;
      		this.maxPosition.y = y;
      		
      		return this;
		}
		winform.getMinMaxInfo = this.updateMinMaxInfo;
		winform.onGetMinMaxInfo = function(hwnd, message, wParam, lParam){ 
			..raw.convert(lParam,this); 
			 
			if(winform.dpiScaleX && winform.dpiScaleY){
				this.minTrackSize.x = minCx / this.dpiScaleX * winform.dpiScaleX;
				this.minTrackSize.y = minCy / this.dpiScaleY * winform.dpiScaleY;	
			}
			else {
				this.minTrackSize.x = minCx;
				this.minTrackSize.y = minCy;
			} 
			
			this.updateMinMaxInfo(); 
			..raw.copy(lParam,this); 
			return 0;
		}; 
	};  
	struct reserved = ::POINT();
	struct maxSize = ::POINT(maxCx,maxCy);
	struct maxPosition = ::POINT();
	struct minTrackSize = ::POINT(minCx,minCy);
	struct maxTrackSize = ::POINT(maxCx,maxCy); 
} 

/**intellisense()
win.ui.minmax = 设置窗口缩放大小范围
win.ui.minmax(.(winform,最小宽度,最小高度,最大宽度,最大高度) = 除第一个参数以外,其他参数可选\n默认最小为当前窗口大小,最大为最大化大小
win.ui.minmax() = !minmaxinfo.
!minmaxinfo.updateMinMaxInfo() = 重新计算窗口缩放大小范围\n!minmaxinfo.
!minmaxinfo.maxSize = !point.
!minmaxinfo.maxPosition = !point.
!minmaxinfo.minTrackSize = !point.
!minmaxinfo.maxTrackSize = !point.
end intellisense**/