Skip to content

Commit 0adabeb

Browse files
committed
add store and restore window pos functions
1 parent 43bba1d commit 0adabeb

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pyrevitlib/pyrevit/script.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
ICON_MEDIUM = 24
4949
ICON_LARGE = 32
5050

51+
5152
def get_info():
5253
"""Return info on current pyRevit command.
5354
@@ -799,3 +800,60 @@ def data_exists(slot_name, this_project=True):
799800
file_ext=DATAFEXT,
800801
add_cmd_name=False)
801802
return os.path.exists(data_file)
803+
804+
805+
def restore_window_position(window, command_name=EXEC_PARAMS.command_name):
806+
"""
807+
Restore window position from saved data.
808+
809+
Args:
810+
window: WPF window instance
811+
command_name: Unique identifier for this window
812+
813+
Returns:
814+
bool: True if position was restored, False if centered to screen
815+
"""
816+
storage_key = "last_window_position_" + command_name
817+
818+
try:
819+
pos = load_data(storage_key, this_project=False)
820+
if not pos:
821+
raise Exception("No saved position")
822+
823+
# Get all screen working areas
824+
all_bounds = [s.WorkingArea for s in framework.Forms.Screen.AllScreens]
825+
x, y = pos.get("Left", 0), pos.get("Top", 0)
826+
827+
# Check if position is visible on any screen
828+
visible = any(
829+
(b.Left <= x <= b.Right and b.Top <= y <= b.Bottom) for b in all_bounds
830+
)
831+
832+
if visible:
833+
window.WindowStartupLocation = (
834+
framework.Windows.WindowStartupLocation.Manual
835+
)
836+
window.Left = x
837+
window.Top = y
838+
return True
839+
else:
840+
raise Exception("Position not visible on any screen")
841+
842+
except Exception:
843+
window.WindowStartupLocation = (
844+
framework.Windows.WindowStartupLocation.CenterScreen
845+
)
846+
return False
847+
848+
849+
def save_window_position(window, command_name=EXEC_PARAMS.command_name):
850+
"""
851+
Save window position to persistent storage.
852+
853+
Args:
854+
window: WPF window instance
855+
command_name: Unique identifier for this window
856+
"""
857+
storage_key = "last_window_position_" + command_name
858+
position_data = {"Left": window.Left, "Top": window.Top}
859+
store_data(storage_key, position_data, this_project=False)

0 commit comments

Comments
 (0)