DefConst('isNewDragArea,
	func(theView)
	begin
		local frame := theView;
		while frame do
		begin
			if (frame = protoTitle or frame = protoNewFolderTab) then
				return true;
			frame := frame._proto;
		end;
		
		return nil;
	end);

DefConst('patchApp,
	func(theApp)
	begin
		//Don't patch a closed app
		
		if not theApp.viewCObject then
			return;
		
		//Make sure we're not already patched
		
		if theApp.(kAppSymbol) then
			return;

		//Make draggable
		
		local kids := theApp:ChildViewFrames();
		foreach kid in kids do
		begin
			if call isNewDragArea with (kid) then
			begin
				local template := EnsureInternal({_proto: nil, viewClickScript: nil, viewFlags: 515});
				template._proto := kid._proto;
				template.viewClickScript := 
					func(unit)
					begin
						if base.DragWindow then
							base:DragWindow(unit);
						else
						begin
							Clicker();
							base:Drag(unit, RelBounds(if not displayParams.appAreaGlobalLeft then 0 else displayParams.appAreaGlobalLeft,
								if not displayParams.appAreaGlobalTop then 0 else displayParams.appAreaGlobalTop,
								displayParams.appAreaWidth, displayParams.appAreaHeight));
						end;
						true;
					end;
				kid._proto := template;
				SetValue(kid, 'viewFlags, 515); // Let the view system drag it
			end;
		end;
		
		local tTemplate :=
			EnsureInternal({_proto: nil, viewQuitScript: nil, tag: kAppSymbol});
		tTemplate._proto := theApp._proto;
		tTemplate.(kAppSymbol) := tTemplate;
		tTemplate.viewQuitScript :=
			func()
			begin
				local result := inherited:?viewQuitScript();
				
				// Remove me from the star, if I'm there
				
				local list := GetRoot().notifyIcon.messageList;
				if list and Length(list) > 0 then
					foreach task in list do
						if task.fn.tag = kAppSymbol and self = task.args[0] then
							GetRoot().notifyIcon:KillAction(task);
				
				// Then unpatch me
				
				if self.(kAppSymbol) then	
					ReplaceObject(self.(kAppSymbol), self.(kAppSymbol)._proto);
				
				return result;
			end;
		theApp._proto := tTemplate;
	end);