Skip to content

Instantly share code, notes, and snippets.

@eel3
Created February 26, 2020 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eel3/1f7f898ae13706f71771aa01c47770ad to your computer and use it in GitHub Desktop.
Save eel3/1f7f898ae13706f71771aa01c47770ad to your computer and use it in GitHub Desktop.
GUI for workperiod.ps1
@echo off
start /min powershell -ExecutionPolicy RemoteSigned -Sta -WindowStyle Hidden -File "%~dpn0.ps1" %*
<#
.SYNOPSIS
Calculate an optimul project work period from a man months (GUI version).
.INPUTS
None.
.OUTPUTS
None.
.EXAMPLE
C:\PS> powershell -ExecutionPolicy RemoteSigned
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\PS> Get-ChildItem *.ps1 | Select-Object Name
Name
----
workperiod.ps1
workperiodgui.ps1
PS C:\PS> .\workperiodgui.ps1
#>
Set-StrictMode -Version Latest
. "$(Split-Path -Parent $script:MyInvocation.MyCommand.Path)\workperiod.ps1"
Add-Type -AssemblyName PresentationFramework
# Configurations
Set-Variable -Name DEFAULT_FACTOR_INDEX -Value 0 -Option Constant
Set-Variable -Name DEFAULT_MAN_MONTH -Value 12 -Option Constant
Set-Variable -Name MAX_MAN_MONTH -Value 120 -Option Constant
# Constants
Set-Variable -Name APP_TITLE -Value 'WorkPeriod GUI' -Option Constant
Set-Variable -Name GUI_XAML -Value @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="$APP_TITLE"
Width="330" Height="330"
MinWidth="330" MinHeight="330">
<StackPanel>
<Menu>
<MenuItem Header="_File">
<MenuItem Name="exitMenuItem" Header="_Exit" />
</MenuItem>
</Menu>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel Margin="10,0,10,0"
FocusManager.FocusedElement="{Binding ElementName=factorComboBox}">
<TextBlock>Factor</TextBlock>
<ComboBox Name="factorComboBox" SelectedIndex="$DEFAULT_FACTOR_INDEX" />
<TextBlock>Project total man-months</TextBlock>
<Slider Name="manMonthSlider"
Minimum="1" Maximum="$MAX_MAN_MONTH" Value="$DEFAULT_MAN_MONTH"
SmallChange="1" LargeChange="10" TickFrequency="1" IsSnapToTickEnabled="True" />
<TextBlock Text="{Binding ElementName=manMonthSlider, Path=Value}" />
<Separator Margin="0,5,0,5" />
<TextBlock>Development work period (month)</TextBlock>
<TextBox Name="period" IsReadOnly="True" />
<TextBlock>Head count</TextBlock>
<TextBox Name="headCount" IsReadOnly="True" />
<Separator Margin="0,10,0,5" />
<TextBlock>Minimum work period (month)</TextBlock>
<TextBox Name="minPeriod" IsReadOnly="True" />
<TextBlock>Head count for minimum work period</TextBlock>
<TextBox Name="maxHeadCount" IsReadOnly="True" />
</StackPanel>
</ScrollViewer>
</StackPanel>
</Window>
"@ -Option Constant
# Functions
function Update-Value($window, $factorIndex, $manMonth)
{
$workPeriod = [WorkPeriod]::new([Factor] $factorIndex)
$period = $window.FindName('period')
$period.Text = $workPeriod.Period($manMonth)
$headCount = $window.FindName('headCount')
$headCount.Text = $workPeriod.HeadCount($manMonth)
$minPeriod = $window.FindName('minPeriod')
$minPeriod.Text = $workPeriod.MinPeriod($manMonth)
$maxHeadCount = $window.FindName('maxHeadCount')
$maxHeadCount.Text = $workPeriod.MaxHeadCount($manMonth)
}
function Show-MainWindow
{
$window = [System.Windows.Markup.XamlReader]::Parse($GUI_XAML)
$menuItem = $window.FindName('exitMenuItem')
$menuItem.Add_Click({
$window.Close()
})
$comboBox = $window.FindName('factorComboBox')
$comboBox.ItemsSource = [System.Enum]::GetNames([Factor])
$comboBox.Add_SelectionChanged({
Update-Value $window $comboBox.SelectedIndex $slider.Value
})
$slider = $window.FindName('manMonthSlider')
$slider.Add_ValueChanged({
Update-Value $window $comboBox.SelectedIndex $slider.Value
})
Update-Value $window $comboBox.SelectedIndex $slider.Value
$window.ShowDialog() | Out-Null
}
# Main routine
Show-MainWindow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment