Collapse and Restore Dual-Pane Layout

Discuss and share scripts and script files...
Post Reply
Norn
Posts: 521
Joined: 24 Oct 2021 16:10

Collapse and Restore Dual-Pane Layout

Post by Norn »

I saw a user's question about this, but the post seems to have been deleted.

This was a test of the ability of free AI models to write XYplorer scripts. The results were not very satisfactory. I provided most of the ideas and logic throughout the process, while the AI helped with the code.

The script calculates the collapsed window size and position based on the breadcrumb bar. Run it again to restore the original dual-pane layout.

Code: Select all

	// Collapse and Restore Dual-Pane Layout (26.11.10)
	perm $p_DP;

	if ($p_DP != "") {
		// Restore original window and dual-pane state.
		explode($v, $p_DP); $left = $v[0]; $top = $v[1]; $width = $v[2]; $height = $v[3]; $pane = $v[4]; $horizontal = $v[5]; $maximized = $v[6];

		// Detect current window state.
		explode($v, controlposition("XY")); $currentLeft = $v[0]; $currentTop = $v[1]; $currentWidth = $v[2]; $currentHeight = $v[3];
		$screen = get("Screen");
		$currentMaximized = IsMaximized($currentWidth, $currentHeight, $screen);

		if ($maximized && !$currentMaximized) {
			#1028;
		} elseif (!$maximized && !$currentMaximized) {
			// Restore original size if it fits at the current position.
			explode($s, $screen); $screenWidth = $s[0]; $screenHeight = $s[1];
			$right = $currentLeft + $width;
			$bottom = $currentTop + $height;

			if ($right <= $screenWidth && $bottom <= $screenHeight) {
				winsize $width, $height;
			} else {
				winpos $left, $top;
				winsize $width, $height;
			}
		}

		if (get("#801") != $horizontal) { #801; }
		dualpane(1, $pane + 4);

		$p_DP = "";

	} elseif (dualpane()) {
		// Get original window and screen dimensions.
		explode($v, controlposition("XY")); $left = $v[0]; $top = $v[1]; $width = $v[2]; $height = $v[3];

		$screen = get("Screen");
		$maximized = IsMaximized($width, $height, $screen);

		$pane = get("Pane");
		$horizontal = get("#801");

		// Save original state.
		$savedState = $left . "|" . $top . "|" . $width . "|" . $height . "|" . $pane . "|" . $horizontal . "|" . $maximized;

		// Initialize temporary variables.
		$rect = ""; $bc1 = ""; $newLeft = 0; $newTop = 0; $newWidth = 0; $newHeight = 0;

		// Get window border.
		$bc1 = controlposition("BC 1"); $border = 0;
		if ($bc1 != "") {
			explode($v, $bc1); $bc1X = $v[0];
			$border = $bc1X - $left;
		}

		// Get pane dimensions before collapsing.
		if ($horizontal) {   // Top/Bottom panes
			if ($pane == 1) {
				$rect = controlposition("L 1");

				if ($rect != "") {
					$statusBar = controlposition("SB"); if ($statusBar) {explode($SB, $statusBar); $statusBarHeight = $SB[3]} else {$statusBarHeight = 0}

					explode($v, $rect); $listY = $v[1]; $listHeight = $v[3];
					$newLeft = $left;
					$newTop = $top;
					$newWidth = $width;
					$newHeight = $listY - $top + $listHeight + $statusBarHeight + $border;
				}

			} else {
				$bc2 = controlposition("BC 2");

				if ($bc1 != "" && $bc2 != "") {
					explode($v, $bc1); $bc1Y = $v[1];
					$nonListHeight = $bc1Y - $top;

					explode($v, $bc2); $bc2Y = $v[1];
					$newLeft = $left;
					$newTop = $bc2Y - $nonListHeight;
					$newWidth = $width;
					$newHeight = $height + $bc1Y - $bc2Y;
					$rect = $bc2;
				}
			}

		} else {   // Left/Right panes
			$rect = controlposition("BC " . $pane);

			if ($rect != "" && $bc1 != "") {
				explode($v, $rect); $paneX = $v[0]; $paneWidth = $v[2];
				$newLeft = $pane == 1 ? $left : $paneX - $border;
				$newTop = $top;
				$newWidth = $paneWidth + $border * 2;
				$newHeight = $height;
			}
		}

		if ($rect != "" && $newWidth > 0 && $newHeight > 0) {
			if ($maximized) {
				// Restore window temporarily after calculating pane dimensions.
				#1029;
				// Adjust window borders when collapsing from maximized state.
				if ($pane == 1) {
					$newTop = $newTop + $border;
					$newHeight = $horizontal ? $newHeight : $newHeight - $border;
				} else {
					$newTop = $horizontal ? $newTop : $newTop + $border;
					$newHeight = $horizontal ? $newHeight : $newHeight - $border;
				}
				wait 100;
			}

			dualpane(0);
			winpos $newLeft, $newTop;
			winsize $newWidth, $newHeight;
			$p_DP = $savedState;
		}
	}

	function IsMaximized($width, $height, $screen) {
		explode($s, $screen); $screenWidth = $s[0]; $screenHeight = $s[1]; $screenDPI = $s[2];
		$margin = $screenDPI / 2;
		$diffWidth = $screenWidth - $width;
		$diffHeight = $screenHeight - $height;

		return $diffWidth >= -$margin && $diffWidth <= $margin
			&& $diffHeight >= -$margin && $diffHeight <= $margin;
	}
Windows 11 24H2 @100% 2560x1440

Post Reply