Skip to content

Commit

Permalink
Add size unit support for new virtual hard disk and resize virtual ha…
Browse files Browse the repository at this point in the history
…rd disk UI.
  • Loading branch information
MouriNaruto committed Jul 16, 2024
1 parent adb6d0f commit 1ef2633
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 21 deletions.
24 changes: 23 additions & 1 deletion NanaBox/NewVirtualHardDiskPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,32 @@ namespace winrt::NanaBox::implementation
UNREFERENCED_PARAMETER(sender);
UNREFERENCED_PARAMETER(e);

const std::uint64_t UnitFactors[] =
{
1ULL, // B
1000ULL, // KB
(1ULL << 10), // KiB
1000000ULL, // MB
(1ULL << 20), // MiB
1000000000ULL, // GB
(1ULL << 30), // GiB
1000000000000ULL, // RB
(1ULL << 40), // TiB
};
const std::size_t MaximumUnitFactorsIndex =
sizeof(UnitFactors) / sizeof(*UnitFactors) - 1;

winrt::hstring Path =
this->FileNameTextBox().Text();
std::uint64_t Size =
std::uint64_t OriginalSize =
std::stoull(this->SizeTextBox().Text().c_str());
std::int32_t UnitIndex =
this->SizeUnitComboBox().SelectedIndex();
if (UnitIndex > MaximumUnitFactorsIndex)
{
UnitIndex = MaximumUnitFactorsIndex;
}
std::uint64_t Size = OriginalSize * UnitFactors[UnitIndex];

winrt::hstring SuccessInstructionText =
Mile::WinRT::GetLocalizedString(
Expand Down
31 changes: 26 additions & 5 deletions NanaBox/NewVirtualHardDiskPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,32 @@
<TextBlock
x:Uid="/NewVirtualHardDiskPage/SizeTextBlock"
Padding="0,2"
Text="Size (in bytes, at least 3 MiB and must be a multiple of 512)" />
<TextBox
x:Name="SizeTextBox"
BeforeTextChanging="NaturalNumberTextBoxBeforeTextChanging"
Text="0" />
Text="Size (at least 3 MiB and must be a multiple of 512 bytes)" />
<Grid Padding="0,2,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="144" />
</Grid.ColumnDefinitions>
<TextBox
x:Name="SizeTextBox"
BeforeTextChanging="NaturalNumberTextBoxBeforeTextChanging"
Text="0" />
<ComboBox
x:Name="SizeUnitComboBox"
Grid.Column="1"
HorizontalAlignment="Stretch"
SelectedIndex="5">
<x:String>B</x:String>
<x:String>KB (1000 B)</x:String>
<x:String>KiB (1024 B)</x:String>
<x:String>MB (1000 KB)</x:String>
<x:String>MiB (1024 KiB)</x:String>
<x:String>GB (1000 MB)</x:String>
<x:String>GiB (1024 MiB)</x:String>
<x:String>TB (1000 GB)</x:String>
<x:String>TiB (1024 GiB)</x:String>
</ComboBox>
</Grid>
</StackPanel>
<Grid Grid.Row="1" Padding="24">
<Grid.Background>
Expand Down
26 changes: 24 additions & 2 deletions NanaBox/ResizeVirtualHardDiskPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,32 @@ namespace winrt::NanaBox::implementation
UNREFERENCED_PARAMETER(sender);
UNREFERENCED_PARAMETER(e);

const std::uint64_t UnitFactors[] =
{
1ULL, // B
1000ULL, // KB
(1ULL << 10), // KiB
1000000ULL, // MB
(1ULL << 20), // MiB
1000000000ULL, // GB
(1ULL << 30), // GiB
1000000000000ULL, // RB
(1ULL << 40), // TiB
};
const std::size_t MaximumUnitFactorsIndex =
sizeof(UnitFactors) / sizeof(*UnitFactors) - 1;

winrt::hstring Path =
this->FileNameTextBox().Text();
std::uint64_t NewSize =
std::uint64_t OriginalSize =
std::stoull(this->SizeTextBox().Text().c_str());
std::int32_t UnitIndex =
this->SizeUnitComboBox().SelectedIndex();
if (UnitIndex > MaximumUnitFactorsIndex)
{
UnitIndex = MaximumUnitFactorsIndex;
}
std::uint64_t Size = OriginalSize * UnitFactors[UnitIndex];

winrt::hstring SuccessInstructionText =
Mile::WinRT::GetLocalizedString(
Expand All @@ -160,7 +182,7 @@ namespace winrt::NanaBox::implementation
{
DWORD Error = ::SimpleResizeVirtualDisk(
Path.c_str(),
NewSize);
Size);

if (ERROR_SUCCESS == Error)
{
Expand Down
31 changes: 26 additions & 5 deletions NanaBox/ResizeVirtualHardDiskPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,32 @@
<TextBlock
x:Uid="/ResizeVirtualHardDiskPage/SizeTextBlock"
Padding="0,2"
Text="[New Size (in bytes)]" />
<TextBox
x:Name="SizeTextBox"
BeforeTextChanging="NaturalNumberTextBoxBeforeTextChanging"
Text="0" />
Text="[Size (at least 3 MiB and must be a multiple of 512 bytes)]" />
<Grid Padding="0,2,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="144" />
</Grid.ColumnDefinitions>
<TextBox
x:Name="SizeTextBox"
BeforeTextChanging="NaturalNumberTextBoxBeforeTextChanging"
Text="0" />
<ComboBox
x:Name="SizeUnitComboBox"
Grid.Column="1"
HorizontalAlignment="Stretch"
SelectedIndex="5">
<x:String>B</x:String>
<x:String>KB (1000 B)</x:String>
<x:String>KiB (1024 B)</x:String>
<x:String>MB (1000 KB)</x:String>
<x:String>MiB (1024 KiB)</x:String>
<x:String>GB (1000 MB)</x:String>
<x:String>GiB (1024 MiB)</x:String>
<x:String>TB (1000 GB)</x:String>
<x:String>TiB (1024 GiB)</x:String>
</ComboBox>
</Grid>
</StackPanel>

<Grid Grid.Row="1" Padding="24">
Expand Down
4 changes: 2 additions & 2 deletions NanaBox/Strings/en/NewVirtualHardDiskPage.resw
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
<comment>Create Virtual Hard Disk</comment>
</data>
<data name="SizeTextBlock.Text" xml:space="preserve">
<value>Size (in bytes, at least 3 MiB and must be a multiple of 512)</value>
<comment>Size (in bytes, at least 3 MiB and must be a multiple of 512)</comment>
<value>Size (at least 3 MiB and must be a multiple of 512 bytes)</value>
<comment>Size (at least 3 MiB and must be a multiple of 512 bytes)</comment>
</data>
<data name="SuccessContentText" xml:space="preserve">
<value>The virtual hard disk file at %s has been successfully created.</value>
Expand Down
4 changes: 2 additions & 2 deletions NanaBox/Strings/en/ResizeVirtualHardDiskPage.resw
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
<comment>Resize</comment>
</data>
<data name="SizeTextBlock.Text" xml:space="preserve">
<value>Size (in bytes, at least 3 MiB and must be a multiple of 512)</value>
<comment>Size (in bytes, at least 3 MiB and must be a multiple of 512)</comment>
<value>Size (at least 3 MiB and must be a multiple of 512 bytes)</value>
<comment>Size (at least 3 MiB and must be a multiple of 512 bytes)</comment>
</data>
<data name="SuccessContentText" xml:space="preserve">
<value>The virtual hard disk file at %s has been successfully resize.</value>
Expand Down
4 changes: 2 additions & 2 deletions NanaBox/Strings/zh-Hans/NewVirtualHardDiskPage.resw
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
<comment>Create Virtual Hard Disk</comment>
</data>
<data name="SizeTextBlock.Text" xml:space="preserve">
<value>大小(以字节为单位,至少 3 MiB 且必须是 512 的倍数)</value>
<comment>Size (in bytes, at least 3 MiB and must be a multiple of 512)</comment>
<value>大小(至少 3 MiB 且必须是 512 字节的倍数)</value>
<comment>Size (at least 3 MiB and must be a multiple of 512 bytes)</comment>
</data>
<data name="SuccessContentText" xml:space="preserve">
<value>位于 %s 的虚拟硬盘文件已成功创建。</value>
Expand Down
4 changes: 2 additions & 2 deletions NanaBox/Strings/zh-Hans/ResizeVirtualHardDiskPage.resw
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
<comment>Resize</comment>
</data>
<data name="SizeTextBlock.Text" xml:space="preserve">
<value>大小(以字节为单位,至少 3 MiB 且必须是 512 的倍数)</value>
<comment>Size (in bytes, at least 3 MiB and must be a multiple of 512)</comment>
<value>大小(至少 3 MiB 且必须是 512 字节的倍数)</value>
<comment>Size (at least 3 MiB and must be a multiple of 512 bytes)</comment>
</data>
<data name="SuccessContentText" xml:space="preserve">
<value>位于 %s 的虚拟硬盘文件已成功调整大小。</value>
Expand Down

0 comments on commit 1ef2633

Please sign in to comment.