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
@@ -166,24 +166,7 @@ SettingsWindow::SettingsWindow()
new BMessage(kMsgSwapAutomaticUpdate)); new BMessage(kMsgSwapAutomaticUpdate));
fSwapEnabledCheckBox->SetExplicitAlignment(align); fSwapEnabledCheckBox->SetExplicitAlignment(align);
char sizeStr[16]; fSwapUsageBar = new BStatusBar("swap usage");
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);
BPopUpMenu* menu = new BPopUpMenu("volume menu"); BPopUpMenu* menu = new BPopUpMenu("volume menu");
fVolumeMenuField = new BMenuField("volume menu field", fVolumeMenuField = new BMenuField("volume menu field",
@@ -214,8 +197,7 @@ SettingsWindow::SettingsWindow()
box->SetLabel(fSwapEnabledCheckBox); box->SetLabel(fSwapEnabledCheckBox);
box->AddChild(BLayoutBuilder::Group<>(B_VERTICAL, B_USE_DEFAULT_SPACING) box->AddChild(BLayoutBuilder::Group<>(B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(memoryView) .Add(fSwapUsageBar)
.Add(swapFileView)
.Add(fSwapAutomaticCheckBox) .Add(fSwapAutomaticCheckBox)
.Add(fVolumeMenuField) .Add(fVolumeMenuField)
.Add(fSizeSlider) .Add(fSizeSlider)
@@ -268,6 +250,9 @@ SettingsWindow::SettingsWindow()
#endif #endif
_Update(); _Update();
// TODO: We may want to run this at an interval
_UpdateSwapInfo();
} }
@@ -305,12 +290,11 @@ SettingsWindow::MessageReceived(BMessage* message)
_Update(); _Update();
break; break;
case kMsgSliderUpdate: case kMsgSliderUpdate:
fSettings.SetSwapSize((off_t)fSizeSlider->Value() * kMegaByte); _RecordChoices();
_Update(); _Update();
break; break;
case kMsgVolumeSelected: case kMsgVolumeSelected:
fSettings.SetSwapVolume(((VolumeMenuItem*)fVolumeMenuField _RecordChoices();
->Menu()->FindMarked())->Volume().Device());
_Update(); _Update();
break; break;
case kMsgSwapEnabledUpdate: case kMsgSwapEnabledUpdate:
@@ -338,13 +322,13 @@ SettingsWindow::MessageReceived(BMessage* message)
} }
} }
fSettings.SetSwapEnabled(fSwapEnabledCheckBox->Value()); _RecordChoices();
_Update(); _Update();
break; break;
} }
case kMsgSwapAutomaticUpdate: case kMsgSwapAutomaticUpdate:
{ {
fSettings.SetSwapAutomatic(fSwapAutomaticCheckBox->Value()); _RecordChoices();
_Update(); _Update();
break; break;
} }
@@ -359,6 +343,8 @@ bool
SettingsWindow::QuitRequested() SettingsWindow::QuitRequested()
{ {
fSettings.SetWindowPosition(Frame().LeftTop()); fSettings.SetWindowPosition(Frame().LeftTop());
_RecordChoices();
fSettings.WriteWindowSettings(); fSettings.WriteWindowSettings();
fSettings.WriteSwapSettings(); fSettings.WriteSwapSettings();
be_app->PostMessage(B_QUIT_REQUESTED); 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 void
SettingsWindow::_Update() SettingsWindow::_Update()
{ {
@@ -475,3 +472,26 @@ SettingsWindow::_Update()
fVolumeMenuField->SetEnabled(fSettings.SwapEnabled() fVolumeMenuField->SetEnabled(fSettings.SwapEnabled()
&& !fSwapAutomaticCheckBox->Value()); && !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());
}
@@ -15,6 +15,7 @@
#include <MenuItem.h> #include <MenuItem.h>
#include <Slider.h> #include <Slider.h>
#include <StatusBar.h>
#include <Volume.h> #include <Volume.h>
#include <Window.h> #include <Window.h>
@@ -69,7 +70,9 @@ private:
status_t _RemoveVolumeMenuItem(dev_t device); status_t _RemoveVolumeMenuItem(dev_t device);
VolumeMenuItem* _FindVolumeMenuItem(dev_t device); VolumeMenuItem* _FindVolumeMenuItem(dev_t device);
void _RecordChoices();
void _Update(); void _Update();
void _UpdateSwapInfo();
BCheckBox* fSwapEnabledCheckBox; BCheckBox* fSwapEnabledCheckBox;
BCheckBox* fSwapAutomaticCheckBox; BCheckBox* fSwapAutomaticCheckBox;
@@ -78,6 +81,7 @@ private:
BButton* fRevertButton; BButton* fRevertButton;
BStringView* fWarningStringView; BStringView* fWarningStringView;
BMenuField* fVolumeMenuField; BMenuField* fVolumeMenuField;
BStatusBar* fSwapUsageBar;
Settings fSettings; Settings fSettings;
}; };