Skip to content

Commit

Permalink
Fix and improvement to clock widget. Updates to preflet.
Browse files Browse the repository at this point in the history
  • Loading branch information
cafeina committed Aug 6, 2024
1 parent 88a2490 commit 95bfea1
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 196 deletions.
208 changes: 54 additions & 154 deletions src/application/mClockView.cpp
Original file line number Diff line number Diff line change
@@ -1,168 +1,53 @@
// mWindow.cpp
#include <DateFormat.h>
#include <TimeFormat.h>
#include <ctime>
#include "mClockView.h"

const char* mPathToConfigFile3 = "LockWorkstationSettings";
const char* mNameConfigBool = "clock";
const char* mNameConfigColorR = "clockR";
const char* mNameConfigColorG = "clockG";
const char* mNameConfigColorB = "clockB";
const char* mNameConfigPlaceX = "clockX";
const char* mNameConfigPlaceY = "clockY";
const char* mNameConfigFontSize = "fontSize";

const char* mDefaultBoolClock = "0";
const char* mDefaultColorClockR = "0";
const char* mDefaultColorClockG = "0";
const char* mDefaultColorClockB = "0";
const char* mDefaultClockPlaceX = "0";
const char* mDefaultClockPlaceY = "0";
const char* mDefaultFontSize = "8"; //*2

/*************************************************/
/******************* Start ***********************/
/*************************************************/
mClockView::mClockView(BRect rect, const char *name, uint32 resizingMode, uint32 flags)
: BView(BRect(0, 0, 200, 100), "Mariux", B_FOLLOW_TOP | B_FOLLOW_LEFT, B_FORCE_ANTIALIASING | B_WILL_DRAW | B_PULSE_NEEDED)
mClockView::mClockView(BRect rect, LWSettings* settings)
: BView(rect, "Mariux", B_FOLLOW_LEFT_TOP, B_WILL_DRAW | B_PULSE_NEEDED),
mSettings(settings)
{
UserThread = spawn_thread(UserThreadChange_static, "Change Image Thread",
B_URGENT_DISPLAY_PRIORITY, this);
resume_thread(UserThread);
//FILEREAD
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
path.Append(mPathToConfigFile3);
status = file.SetTo(path.Path(), B_READ_ONLY);
if (status == B_OK)
{
status = savemessage.Unflatten(&file);
}

//Bool
if (savemessage.HasString(mNameConfigBool) == 0)
{
savemessage.AddString(mNameConfigBool, mDefaultBoolClock);
}
savemessage.FindString(mNameConfigBool, &mStringBoolClock);

//Red
if (savemessage.HasString(mNameConfigColorR) == 0)
{
savemessage.AddString(mNameConfigColorR, mDefaultColorClockR);
}
savemessage.FindString(mNameConfigColorR, &mStringColorR);

//Green
if (savemessage.HasString(mNameConfigColorG) == 0)
{
savemessage.AddString(mNameConfigColorG, mDefaultColorClockG);
}
savemessage.FindString(mNameConfigColorG, &mStringColorG);

//Blue
if (savemessage.HasString(mNameConfigColorB) == 0)
{
savemessage.AddString(mNameConfigColorB, mDefaultColorClockB);
}
savemessage.FindString(mNameConfigColorB, &mStringColorB);
ThreadedCall(UserThread, UserThreadChange_static, "Change Image Thread",
B_URGENT_DISPLAY_PRIORITY, this);

//PlaceX
if (savemessage.HasString(mNameConfigPlaceX) == 0)
{
savemessage.AddString(mNameConfigPlaceX, mDefaultClockPlaceX);
}
savemessage.FindString(mNameConfigPlaceX, &mStringPlaceX);
_InitUIData();

//PlaceY
if (savemessage.HasString(mNameConfigPlaceY) == 0)
{
savemessage.AddString(mNameConfigPlaceY, mDefaultClockPlaceY);
}
savemessage.FindString(mNameConfigPlaceY, &mStringPlaceY);

//Bool
if (savemessage.HasString(mNameConfigFontSize) == 0)
{
savemessage.AddString(mNameConfigFontSize, mDefaultFontSize);
}
savemessage.FindString(mNameConfigFontSize, &mStringFontSize);

if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE) == B_OK)
{
savemessage.Flatten(&file);
}

mFontSize = atoi(mStringFontSize.String());
mColorR = atoi(mStringColorR.String());
mColorG = atoi(mStringColorG.String());
mColorB = atoi(mStringColorB.String());
mPlaceX = atoi(mStringPlaceX.String());
mPlaceY = atoi(mStringPlaceY.String());
SetHighColor(mColorR, mColorG, mColorB);
SetDrawingMode(B_OP_OVER);
SetFont(be_bold_font);
SetFontSize(((mFontSize)*2));
SetViewColor(B_TRANSPARENT_COLOR);
SetHighColor(mColor);
SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_COMPOSITE_LIGHTEN);
SetFont(be_bold_font);
SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
ResizeToPreferred();
}

/*********************************************/
/***************** Draw **********************/
/*********************************************/
void mClockView::Draw(BRect updateRect)
{
mSecond.SetTo("");
mMinute.SetTo("");
mHour.SetTo("");
//second
if(todaysecond < 10)
{
mSecond << "0" << todaysecond;
}
else
{
mSecond << todaysecond;
}
//minute
if(todayminute < 10)
{
mMinute << "0" << todayminute;
}
else
{
mMinute << todayminute;
}
//hour
if(todayhour < 10)
{
mHour << "0" << todayhour;
}
else
{
mHour << todayhour;
}
if (mStringBoolClock == "0")
{
mShowClock = false;
}
else
{
mShowClock = true;
}
mTimer.SetTo("");
if (mShowClock)
{
mTimer << mHour.String() << ":" << mMinute.String() << ":" << mSecond.String();
}
MovePenTo((mPlaceX+2), (mPlaceY+15));
DrawString(mTimer.String());
SetFontSize(mFontSize * 8);
float timewidth = StringWidth(mTimer.String());
MovePenTo((Frame().Width() / 2) - (timewidth / 2),
(Frame().Height() * 0.5f));
DrawString(mTimer.String());

SetFontSize(mFontSize * 4);
float datewidth = StringWidth(mDater.String());
MovePenTo((Frame().Width() / 2) - (datewidth / 2),
(Frame().Height()) * 0.9f);
DrawString(mDater.String());

Invalidate();
}

void mClockView::Pulse()
{
time(&tmptodaytime);
TodayTime = localtime(&tmptodaytime);
if (todaysecond != TodayTime->tm_sec)
{
Invalidate();
}
_TimeUpdate();
Invalidate();
}

int32
Expand All @@ -176,15 +61,30 @@ mClockView::UserThreadChange_static(void *data)

void mClockView::UserThreadChangeImage()
{
int a = 1;
while (a == 1)
{
time(&tmptodaytime);
TodayTime = localtime(&tmptodaytime);
todaysecond = TodayTime->tm_sec;
todayminute = TodayTime->tm_min;
todayhour = TodayTime->tm_hour;
sleep(1);
while(mShowClock) {
_TimeUpdate();

mTimer.SetTo("");
BTimeFormat tfmt;
tfmt.Format(mTimer, tmptodaytime, B_MEDIUM_TIME_FORMAT, NULL);

mDater.SetTo("");
BDateFormat fmt;
fmt.Format(mDater, tmptodaytime, B_LONG_DATE_FORMAT, NULL);

sleep(1);
}
}

void mClockView::_InitUIData()
{
mShowClock = mSettings->ClockIsEnabled();
mColor = mSettings->ClockColor();
mPlace = mSettings->ClockLocation();
mFontSize = mSettings->ClockSize();
}

void mClockView::_TimeUpdate()
{
std::time(&tmptodaytime);
}
44 changes: 19 additions & 25 deletions src/application/mClockView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,36 @@
#define _mClockView_h_

#include "mConstant.h"
#include "../common/LockWorkstationConfig.h"
#include "../common/ThreadedClass.h"

class mClockView
: public BView
: public BView, ThreadedClass
{
public:
mClockView(BRect rect, const char *name, uint32 resizingMode, uint32 flags);
mClockView(BRect rect, LWSettings* settings);
void Draw(BRect updateRect);
virtual void Pulse();

private:
time_t tmptodaytime;
char charer[1024];
int todaysecond , todayminute , todayhour , todayday , todaymonth , todayyear ;
int mColorR, mColorG, mColorB, mPlaceX, mPlaceY, mFontSize;
void _InitUIData();
void _TimeUpdate();
private:
// From settings
LWSettings *mSettings;
int mFontSize;
bool mShowClock;
struct tm *TodayTime;
BString mTimer;
BString mMinute;
BString mHour;
BString mSecond;
BPath path;
BFile file;
status_t status;
BMessage savemessage;
BString mStringBoolClock;
BString mStringColorR;
BString mStringColorG;
BString mStringColorB;
BString mStringPlaceX;
BString mStringPlaceY;
BString mStringFontSize;
mClockView* mCV;
rgb_color mColor;
BPoint mPlace;

// Date & time
time_t tmptodaytime;
BString mTimer,
mDater;

//threads
thread_id UserThread;
static int32 UserThreadChange_static(void *data);
void UserThreadChangeImage();
};

#endif
#endif
34 changes: 24 additions & 10 deletions src/application/mWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,42 @@ mWindow::mWindow(const char* mWindowTitle)
if(!settings->SessionBarIsEnabled())
sessionbar->Hide();

mClock = new mClockView(BRect(0, 0, 100, 100), settings);
if(!settings->ClockIsEnabled())
mClock->Hide();

// Background view
mView = new mBackgroundView(BRect(0, 0, 2000, 2000), NULL, B_FOLLOW_NONE,
B_WILL_DRAW, settings->BackgroundColor(), settings->BackgroundMode(),
imgpath, settings->BackgroundImageSnooze());

BLayoutBuilder::Group<>(mView, B_HORIZONTAL, 0)
// Layout kit
BLayoutBuilder::Group<>(mView, B_VERTICAL, 0)
.SetInsets(B_USE_WINDOW_INSETS)
.AddGlue()
.AddGroup(B_VERTICAL)
.AddGlue(0.1f)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING, 0.1f)
.AddGlue()
.Add(loginbox)
.AddStrut(12.0f)
.Add(sessionbar)
.Add(mClock)
.AddGlue()
.End()
.AddGlue()
.AddGroup(B_VERTICAL)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING, 0.7f)
.AddGlue()
.AddGroup(B_VERTICAL)
.AddGlue()
.Add(loginbox)
.AddStrut(12.0f)
.Add(sessionbar)
.AddGlue()
.End()
.AddGlue()
.Add(infoview)
.AddGroup(B_VERTICAL)
.AddGlue()
.Add(infoview)
.AddGlue()
.End()
.AddGlue()
.End()
.AddGlue()
.AddGlue(0.1f)
.End();

AddChild(mView);
Expand Down
1 change: 1 addition & 0 deletions src/application/mWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class mWindow : public BWindow
mLoginBox *loginbox;
mSystemInfo *infoview;
mSessionBar *sessionbar;
mClockView *mClock;

LWSettings *settings;
mLogger *logger;
Expand Down
2 changes: 1 addition & 1 deletion src/preflet/mUserInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mUserInfo::mUserInfo(BRect frame, BWindow* parent, const char* name)
.End();
containerView->Hide();

fBtShowPwdChangeView = new BButton("bt_change", B_TRANSLATE("Change pass"),
fBtShowPwdChangeView = new BButton("bt_change", B_TRANSLATE("Password"),
new BMessage(UI_WINDOW_SHOW_PWDCHANGE));
fBtShowPwdChangeView->SetBehavior(BButton::B_TOGGLE_BEHAVIOR);
fBtCloseWindow = new BButton("bt_ok", B_TRANSLATE("Cerrar"),
Expand Down
Loading

0 comments on commit 95bfea1

Please sign in to comment.