Dock-PS.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<#
.Synopsis
    Move the host into a docking position or undock the host.

.Description
    Depending on "Position" parameter, the host will either
    become a docked window (Application Desktop Toolbar) or
    be restored to a usual window.

    "dock" is an alias of this cmdlet.

    Notice: You MUST use "exit" command to quit the host if
    the host is docked, otherwise the docking position will
    not be reclaimed by the system immediately after the
    termination of the process.

.Parameter Position
    Default parameter, default value is "Right".

    Possible values are "Left", "Right", "Top", "Bottom" and
    "Center".

    If the value is "Center", the host will be undocked.
    Otherwise, the host is docked into "Position" of the
    primary screen.

.Example
    Move-Host Left

    If the host is not docked, this docks the host to the
    left of the screen. If the host is already docked, it
    is moved to the left of the screen if not already there.

.Example
    dock Center

    If the host is docked, this undocks the host. Otherwise,
    it does nothing.

    It is also possible to use "Pop-Host" cmdlet to undock
    the host.

#>

Function Move-Host
{
    [Alias('dock')]
    [CmdletBinding(HelpUri = 'https://github.com/GeeLaw/PowerShellThingies/tree/master/modules/Dock-PS')]
    Param
    (
        [ValidateSet('Left', 'Right', 'Top', 'Bottom', 'Center')]
        [string]$Position = 'Right'
    )
    Process
    {
        $Position = $Position.ToLower();
        If ($Position -eq 'center')
        {
            Pop-Host;
            Return;
        }
        [DockPSHelper.AppBarHelper]::AddAppBarWindow([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle, $Position);
        [DockPSHelper.AppBarHelper]::MoveAppBarWindow([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle, $Position);
    }
}

<#
.Synopsis
    Undock the host.

.Description
    If the host is docked, this undocks the host. Otherwise,
    it does nothing. Equivalent to "Move-Host -Position Center".

    "undock" is an alias of this cmdlet.

.Example
    Pop-Host

    If the host is docked, this undocks the host. Otherwise,
    it does nothing.

    It is also possible to use "Move-Host" cmdlet with Position
    being "Center" to undock the host.

#>

Function Pop-Host
{
    [Alias('undock')]
    [CmdletBinding(HelpUri = 'https://github.com/GeeLaw/PowerShellThingies/tree/master/modules/Dock-PS')]
    Param()
    Process
    {
        [DockPSHelper.AppBarHelper]::RemoveAppBarWindow([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle);
    }
}

<#
.Synopsis
    Resize the docked host.

.Description
    If the host is docked, this resizes the dock. The size
    is in the percentage of the primary screen size.

    "resize" is an alias of this cmdlet.

    The input value(s) might be adjusted by the docking manager
    so that the window has reasonable size. However, if the
    input value(s) is/are 0, the corresponding size will not be
    modified.

.Parameter Both
    The percentage of either height or width, whichever applies
    to the docking position.

    Notice: The value is multiplied by 100.

.Parameter Height
    The height. This can be set while the host is in all four
    docking positions, but only exhibits immediate effect if
    the host is docked to the top/bottom of the screen.

.Parameter Width
    The width. This can be set while the host is in all four
    docking positions, but only exhibits immediate effect if
    the host is docked to the left/right of the screen.

.Example
    Resize-Host 30

    If the host is docked, this sets the height/width of the
    "free edge" of the host to 30% of the primary screen.
    Otherwise, it does nothing.

.Example
    Resize-Host -Height 30

    If the host is docked, this sets the height of of the
    host to 30% of the primary screen height, which is in
    effect when the host is docked top the top/bottom of
    the screen. Otherwise, it does nothing.

#>

Function Resize-Host
{
    [Alias('resize')]
    [CmdletBinding(HelpUri = 'https://github.com/GeeLaw/PowerShellThingies/tree/master/modules/Dock-PS', DefaultParameterSetName = 'Both', PositionalBinding = $false)]
    Param
    (
        [Parameter(Mandatory = $true, ParameterSetName = 'Both', Position = 0)]
        [ValidateRange(0, 100)]
        [int]$Both,
        [Parameter(ParameterSetName = 'Separate', Position = 0)]
        [ValidateRange(0, 100)]
        [int]$Width = 0,
        [Parameter(ParameterSetName = 'Separate', Position = 1)]
        [ValidateRange(0, 100)]
        [int]$Height = 0
    )
    Process
    {
        If ($PSCmdlet.ParameterSetName -eq 'Both')
        {
            [DockPSHelper.AppBarHelper]::ResizeAppBarWindow([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle, $Both / 100.0, $Both / 100.0);
        }
        Else
        {
            [DockPSHelper.AppBarHelper]::ResizeAppBarWindow([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle, $Width / 100.0, $Height / 100.0);
        }
    }
}

Export-ModuleMember -Function @('Move-Host', 'Pop-Host', 'Resize-Host') -Alias @('dock', 'undock', 'resize') -Cmdlet @() -Variable @();