VM Preflet: Add a BStatusBar to show swap file usage

* Correct a situation where disabling the auto swap without
  adjusting the swap size would result in an invalid swap
  size getting written to the configuration
This commit is contained in:
Alexander von Gluck IV 2012-09-07 12:40:56 -05:00
parent 5e7287f987
commit 59c595739c
2 changed files with 49 additions and 25 deletions

View File

@ -166,24 +166,7 @@ SettingsWindow::SettingsWindow()
new BMessage(kMsgSwapAutomaticUpdate));
fSwapEnabledCheckBox->SetExplicitAlignment(align);
char sizeStr[16];
system_info info;
get_system_info(&info);
BString string = B_TRANSLATE("Physical memory: ");
string << string_for_size(info.max_pages * B_PAGE_SIZE, sizeStr,
sizeof(sizeStr));
BStringView* memoryView = new BStringView("physical memory",
string.String());
memoryView->SetExplicitAlignment(align);
system_memory_info memInfo = {};
__get_system_info_etc(B_MEMORY_INFO, &memInfo, sizeof(memInfo));
string = B_TRANSLATE("Current swap file size: ");
string << string_for_size(memInfo.max_swap_space, sizeStr,
sizeof(sizeStr));
BStringView* swapFileView = new BStringView("current swap size",
string.String());
swapFileView->SetExplicitAlignment(align);
fSwapUsageBar = new BStatusBar("swap usage");
BPopUpMenu* menu = new BPopUpMenu("volume menu");
fVolumeMenuField = new BMenuField("volume menu field",
@ -214,8 +197,7 @@ SettingsWindow::SettingsWindow()
box->SetLabel(fSwapEnabledCheckBox);
box->AddChild(BLayoutBuilder::Group<>(B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(memoryView)
.Add(swapFileView)
.Add(fSwapUsageBar)
.Add(fSwapAutomaticCheckBox)
.Add(fVolumeMenuField)
.Add(fSizeSlider)
@ -268,6 +250,9 @@ SettingsWindow::SettingsWindow()
#endif
_Update();
// TODO: We may want to run this at an interval
_UpdateSwapInfo();
}
@ -305,12 +290,11 @@ SettingsWindow::MessageReceived(BMessage* message)
_Update();
break;
case kMsgSliderUpdate:
fSettings.SetSwapSize((off_t)fSizeSlider->Value() * kMegaByte);
_RecordChoices();
_Update();
break;
case kMsgVolumeSelected:
fSettings.SetSwapVolume(((VolumeMenuItem*)fVolumeMenuField
->Menu()->FindMarked())->Volume().Device());
_RecordChoices();
_Update();
break;
case kMsgSwapEnabledUpdate:
@ -338,13 +322,13 @@ SettingsWindow::MessageReceived(BMessage* message)
}
}
fSettings.SetSwapEnabled(fSwapEnabledCheckBox->Value());
_RecordChoices();
_Update();
break;
}
case kMsgSwapAutomaticUpdate:
{
fSettings.SetSwapAutomatic(fSwapAutomaticCheckBox->Value());
_RecordChoices();
_Update();
break;
}
@ -359,6 +343,8 @@ bool
SettingsWindow::QuitRequested()
{
fSettings.SetWindowPosition(Frame().LeftTop());
_RecordChoices();
fSettings.WriteWindowSettings();
fSettings.WriteSwapSettings();
be_app->PostMessage(B_QUIT_REQUESTED);
@ -417,6 +403,17 @@ SettingsWindow::_FindVolumeMenuItem(dev_t device)
}
void
SettingsWindow::_RecordChoices()
{
fSettings.SetSwapAutomatic(fSwapAutomaticCheckBox->Value());
fSettings.SetSwapEnabled(fSwapEnabledCheckBox->Value());
fSettings.SetSwapSize((off_t)fSizeSlider->Value() * kMegaByte);
fSettings.SetSwapVolume(((VolumeMenuItem*)fVolumeMenuField
->Menu()->FindMarked())->Volume().Device());
}
void
SettingsWindow::_Update()
{
@ -475,3 +472,26 @@ SettingsWindow::_Update()
fVolumeMenuField->SetEnabled(fSettings.SwapEnabled()
&& !fSwapAutomaticCheckBox->Value());
}
void
SettingsWindow::_UpdateSwapInfo()
{
system_memory_info memInfo = {};
__get_system_info_etc(B_MEMORY_INFO, &memInfo, sizeof(memInfo));
off_t currentSwapSize = memInfo.max_swap_space;
off_t currentSwapUsed = (memInfo.max_swap_space - memInfo.free_swap_space);
char sizeStr[16];
BString swapSizeStr = string_for_size(currentSwapSize, sizeStr,
sizeof(sizeStr));
BString swapUsedStr = string_for_size(currentSwapUsed, sizeStr,
sizeof(sizeStr));
BString string = swapUsedStr << " / " << swapSizeStr;
fSwapUsageBar->SetMaxValue(currentSwapSize / kMegaByte);
fSwapUsageBar->Update(currentSwapUsed / kMegaByte,
B_TRANSLATE("Current Swap:"), string.String());
}

View File

@ -15,6 +15,7 @@
#include <MenuItem.h>
#include <Slider.h>
#include <StatusBar.h>
#include <Volume.h>
#include <Window.h>
@ -69,7 +70,9 @@ private:
status_t _RemoveVolumeMenuItem(dev_t device);
VolumeMenuItem* _FindVolumeMenuItem(dev_t device);
void _RecordChoices();
void _Update();
void _UpdateSwapInfo();
BCheckBox* fSwapEnabledCheckBox;
BCheckBox* fSwapAutomaticCheckBox;
@ -78,6 +81,7 @@ private:
BButton* fRevertButton;
BStringView* fWarningStringView;
BMenuField* fVolumeMenuField;
BStatusBar* fSwapUsageBar;
Settings fSettings;
};