From 92f2ee4547d88244cd6286a353ed0ca70a0ed5bb Mon Sep 17 00:00:00 2001 From: barrett Date: Thu, 7 Jun 2012 16:46:07 +0000 Subject: [PATCH] Improved the behaviour to don't get focus when a message is received or the user is typing, from that rev the chat window will appear on the screen without leaving the focus of your current application. But there are two exeptions...when a window is created the first time or the user open it from the contacts list it will get focus. Other little fixes, changed variable names and style. --- application/ChatWindow.cpp | 28 +++++++++++ application/ChatWindow.h | 3 ++ application/ContactLinker.cpp | 32 ++++++++----- application/ContactLinker.h | 4 +- application/MainWindow.cpp | 11 +++-- application/Server.cpp | 6 +-- application/preferences/CayaPreferences.cpp | 12 ++--- application/preferences/CayaPreferences.h | 4 +- .../preferences/PreferencesBehavior.cpp | 46 +++++++++---------- application/preferences/PreferencesBehavior.h | 4 +- 10 files changed, 97 insertions(+), 53 deletions(-) diff --git a/application/ChatWindow.cpp b/application/ChatWindow.cpp index 86e4cd5..f331c48 100644 --- a/application/ChatWindow.cpp +++ b/application/ChatWindow.cpp @@ -96,6 +96,20 @@ ChatWindow::ChatWindow(ContactLinker* cl) } +void +ChatWindow::ShowWindow() +{ + if (IsHidden()) + Show(); + + if (IsMinimized()) + Minimize(false); + + if (!IsActive()) + Activate(true); +} + + bool ChatWindow::QuitRequested() { @@ -153,6 +167,7 @@ ChatWindow::MessageReceived(BMessage* message) case IM_MESSAGE: ImMessage(message); break; + default: BWindow::MessageReceived(message); break; @@ -265,3 +280,16 @@ ChatWindow::AppendStatus(CayaStatus status) fReceiveView->Append("\n", COL_TEXT, COL_TEXT, R_TEXT); fReceiveView->ScrollToSelection(); } + + +void +ChatWindow::AvoidFocus(bool avoid) +{ + // This is needed to avoid the window focus when + // a new message is received, since it could be a lot annoying + // for the user + if (avoid) + SetFlags(B_AVOID_FOCUS); + else + SetFlags(Flags() &~ B_AVOID_FOCUS); +} diff --git a/application/ChatWindow.h b/application/ChatWindow.h index ec84c0f..ca94671 100644 --- a/application/ChatWindow.h +++ b/application/ChatWindow.h @@ -20,6 +20,8 @@ class ChatWindow : public BWindow, public Observer { public: ChatWindow(ContactLinker* cl); + virtual void ShowWindow(); + virtual void MessageReceived(BMessage* message); virtual bool QuitRequested(); @@ -32,6 +34,7 @@ public: void ObserveInteger(int32 what, int32 val); void AppendStatus(CayaStatus status); + void AvoidFocus(bool avoid); private: BTextView* fSendView; ContactLinker* fContactLinker; diff --git a/application/ContactLinker.cpp b/application/ContactLinker.cpp index 5e584c6..3144805 100644 --- a/application/ContactLinker.cpp +++ b/application/ContactLinker.cpp @@ -34,7 +34,8 @@ ContactLinker::ContactLinker(BString id, BMessenger msgn) fMessenger(msgn), fLooper(NULL), fStatus(CAYA_OFFLINE), - fPopUp(NULL) + fPopUp(NULL), + fNewWindow(true) { // Create the roster item and register it as observer fRosterItem = new RosterItem(id.String(), this); @@ -59,29 +60,38 @@ ContactLinker::DeleteWindow() UnregisterObserver(fChatWindow); fChatWindow->Quit(); fChatWindow = NULL; + fNewWindow = true; } } } void -ContactLinker::ShowWindow() +ContactLinker::ShowWindow(bool typing, bool userAction) { if (fChatWindow == NULL) CreateChatWindow(); + fChatWindow->AvoidFocus(true); + if (CayaPreferences::Item()->MoveToCurrentWorkspace) fChatWindow->SetWorkspaces(B_CURRENT_WORKSPACE); - if (fChatWindow->IsHidden()) - fChatWindow->Show(); - - if (fChatWindow->IsMinimized()) - fChatWindow->Minimize(false); - - if (CayaPreferences::Item()->FocusOnMessageReceived == true - || CayaPreferences::Item()->FocusUserIsTyping == true) - fChatWindow->Activate(true); + if (fNewWindow || userAction) { + fChatWindow->AvoidFocus(false); + fChatWindow->ShowWindow(); + fNewWindow = false; + } else { + if (typing) { + if (CayaPreferences::Item()->RaiseUserIsTyping) + fChatWindow->ShowWindow(); + } else { + if (CayaPreferences::Item()->RaiseOnMessageReceived + || fChatWindow->IsHidden()) + fChatWindow->ShowWindow(); + } + } + fChatWindow->AvoidFocus(false); } diff --git a/application/ContactLinker.h b/application/ContactLinker.h index 3438855..c2e89de 100644 --- a/application/ContactLinker.h +++ b/application/ContactLinker.h @@ -27,7 +27,7 @@ public: ChatWindow* GetChatWindow(); void DeleteWindow(); - void ShowWindow(); + void ShowWindow(bool typing = false, bool userAction = false); void HideWindow(); void ShowPopUp(BPoint where); @@ -70,6 +70,8 @@ private: BBitmap* fAvatarBitmap; CayaStatus fStatus; ContactPopUp* fPopUp; + + bool fNewWindow; }; #endif // _CONTACT_LINKER_H_ diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index 87dd561..77a2591 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -137,7 +137,8 @@ void MainWindow::MessageReceived(BMessage* message) { switch (message->what) { - case kSearchContact: { + case kSearchContact: + { void* control = NULL; if (message->FindPointer("source", &control) != B_OK) return; @@ -165,16 +166,18 @@ MainWindow::MessageReceived(BMessage* message) } break; } - case CAYA_SHOW_SETTINGS: { + case CAYA_SHOW_SETTINGS: + { PreferencesDialog* dialog = new PreferencesDialog(); dialog->Show(); break; } - case CAYA_OPEN_CHAT_WINDOW: { + case CAYA_OPEN_CHAT_WINDOW: + { int index = message->FindInt32("index"); RosterItem* ritem = ItemAt(index); if (ritem != NULL) - ritem->GetContactLinker()->ShowWindow(); + ritem->GetContactLinker()->ShowWindow(false, true); break; } diff --git a/application/Server.cpp b/application/Server.cpp index 3c2c654..3aee4a6 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -321,8 +321,7 @@ Server::ImMessage(BMessage* msg) ContactLinker* item = fRosterMap.ValueFor(id, &found); if (found) { ChatWindow* win = item->GetChatWindow(); - if (CayaPreferences::Item()->FocusOnMessageReceived) - item->ShowWindow(); + item->ShowWindow(); win->PostMessage(msg); } } @@ -338,8 +337,7 @@ Server::ImMessage(BMessage* msg) ContactLinker* item = fRosterMap.ValueFor(id, &found); if (found) { ChatWindow* win = item->GetChatWindow(); - if (CayaPreferences::Item()->FocusUserIsTyping) - item->ShowWindow(); + item->ShowWindow(true); win->PostMessage(msg); } } diff --git a/application/preferences/CayaPreferences.cpp b/application/preferences/CayaPreferences.cpp index 292bc21..0594224 100644 --- a/application/preferences/CayaPreferences.cpp +++ b/application/preferences/CayaPreferences.cpp @@ -14,8 +14,8 @@ template<> const char* CayaPreferences::fFilename = "preferences"; CayaPreferencesData::CayaPreferencesData() : MoveToCurrentWorkspace(true), - FocusOnMessageReceived(false), - FocusUserIsTyping(false), + RaiseOnMessageReceived(false), + RaiseUserIsTyping(false), HideCayaDeskbar(false), DisableReplicant(true), IgnoreEmoticons(false), @@ -78,8 +78,8 @@ CayaPreferencesData::Flatten(BPositionIO* flatData) const // Behaviour _AddBool(flatData, MoveToCurrentWorkspace); - _AddBool(flatData, FocusOnMessageReceived); - _AddBool(flatData, FocusUserIsTyping); + _AddBool(flatData, RaiseOnMessageReceived); + _AddBool(flatData, RaiseUserIsTyping); _AddBool(flatData, NotifyProtocolStatus); _AddBool(flatData, NotifyContactStatus); @@ -142,8 +142,8 @@ CayaPreferencesData::Unflatten(type_code code, BPositionIO* flatData) // Behaviour MoveToCurrentWorkspace = _ReadBool(flatData); - FocusOnMessageReceived = _ReadBool(flatData); - FocusUserIsTyping = _ReadBool(flatData); + RaiseOnMessageReceived = _ReadBool(flatData); + RaiseUserIsTyping = _ReadBool(flatData); NotifyProtocolStatus = _ReadBool(flatData); NotifyContactStatus = _ReadBool(flatData); diff --git a/application/preferences/CayaPreferences.h b/application/preferences/CayaPreferences.h index c30e227..11393e2 100644 --- a/application/preferences/CayaPreferences.h +++ b/application/preferences/CayaPreferences.h @@ -26,8 +26,8 @@ public: status_t Unflatten(type_code code, BPositionIO* flatData); bool MoveToCurrentWorkspace; - bool FocusOnMessageReceived; - bool FocusUserIsTyping; + bool RaiseOnMessageReceived; + bool RaiseUserIsTyping; bool NotifyProtocolStatus; bool NotifyContactStatus; diff --git a/application/preferences/PreferencesBehavior.cpp b/application/preferences/PreferencesBehavior.cpp index f6864bb..6501182 100644 --- a/application/preferences/PreferencesBehavior.cpp +++ b/application/preferences/PreferencesBehavior.cpp @@ -24,14 +24,14 @@ #include "TheApp.h" const uint32 kToCurrentWorkspace = 'CBcw'; -const uint32 kFocusOnMessageReceived= 'FCmr'; -const uint32 kFocusUserIsTyping = 'FCit'; +const uint32 kRaiseOnMessageReceived = 'FCmr'; +const uint32 kRaiseUserIsTyping = 'FCit'; const uint32 kNotifyProtocolsLogin = 'NTpl'; const uint32 kNotifyContactStatus = 'NTcl'; PreferencesBehavior::PreferencesBehavior() - : BView("Behavior", B_WILL_DRAW) + : BView("Behavior", B_WILL_DRAW) { fOnIncoming = new BStringView("onIncoming", "On incoming message..."); @@ -42,13 +42,13 @@ PreferencesBehavior::PreferencesBehavior() "Move window to current workspace", new BMessage(kToCurrentWorkspace)); - fFocusOnMessageReceived = new BCheckBox("FocusOnMessageReceived", - "Get focus when a message is received", - new BMessage(kFocusOnMessageReceived)); + fRaiseOnMessageReceived = new BCheckBox("FocusOnMessageReceived", + "Auto-raise when a message is received", + new BMessage(kRaiseOnMessageReceived)); - fFocusUserIsTyping = new BCheckBox("FocusUserIsTyping", - "Get focus when user is typing", - new BMessage(kFocusUserIsTyping)); + fRaiseUserIsTyping = new BCheckBox("FocusUserIsTyping", + "Auto-raise when user is typing", + new BMessage(kRaiseUserIsTyping)); fPlaySoundOnMessageReceived = new BCheckBox("PlaySoundOnMessageReceived", "Play sound event", NULL); @@ -82,8 +82,8 @@ PreferencesBehavior::PreferencesBehavior() .Add(fOnIncoming) .AddGroup(B_VERTICAL, spacing) .Add(fToCurrentWorkspace) - .Add(fFocusOnMessageReceived) - .Add(fFocusUserIsTyping) + .Add(fRaiseOnMessageReceived) + .Add(fRaiseUserIsTyping) .Add(fMarkUnreadWindow) .Add(fMarkUnreadReplicant) .Add(fPlaySoundOnMessageReceived) @@ -106,17 +106,17 @@ void PreferencesBehavior::AttachedToWindow() { fToCurrentWorkspace->SetTarget(this); - fFocusUserIsTyping->SetTarget(this); - fFocusOnMessageReceived->SetTarget(this); + fRaiseUserIsTyping->SetTarget(this); + fRaiseOnMessageReceived->SetTarget(this); fNotifyProtocols->SetTarget(this); fNotifyContactStatus->SetTarget(this); fToCurrentWorkspace->SetValue( CayaPreferences::Item()->MoveToCurrentWorkspace); - fFocusUserIsTyping->SetValue( - CayaPreferences::Item()->FocusUserIsTyping); - fFocusOnMessageReceived->SetValue( - CayaPreferences::Item()->FocusOnMessageReceived); + fRaiseUserIsTyping->SetValue( + CayaPreferences::Item()->RaiseUserIsTyping); + fRaiseOnMessageReceived->SetValue( + CayaPreferences::Item()->RaiseOnMessageReceived); fNotifyProtocols->SetValue( CayaPreferences::Item()->NotifyProtocolStatus); fNotifyContactStatus->SetValue( @@ -132,13 +132,13 @@ PreferencesBehavior::MessageReceived(BMessage* message) CayaPreferences::Item()->MoveToCurrentWorkspace = fToCurrentWorkspace->Value(); break; - case kFocusOnMessageReceived: - CayaPreferences::Item()->FocusOnMessageReceived - = fFocusOnMessageReceived->Value(); + case kRaiseOnMessageReceived: + CayaPreferences::Item()->RaiseOnMessageReceived + = fRaiseOnMessageReceived->Value(); break; - case kFocusUserIsTyping: - CayaPreferences::Item()->FocusUserIsTyping - = fFocusUserIsTyping->Value(); + case kRaiseUserIsTyping: + CayaPreferences::Item()->RaiseUserIsTyping + = fRaiseUserIsTyping->Value(); break; case kNotifyProtocolsLogin: CayaPreferences::Item()->NotifyProtocolStatus diff --git a/application/preferences/PreferencesBehavior.h b/application/preferences/PreferencesBehavior.h index 1b22a9e..5465098 100644 --- a/application/preferences/PreferencesBehavior.h +++ b/application/preferences/PreferencesBehavior.h @@ -22,8 +22,8 @@ private: BStringView* fOnIncoming; BCheckBox* fToCurrentWorkspace; - BCheckBox* fFocusOnMessageReceived; - BCheckBox* fFocusUserIsTyping; + BCheckBox* fRaiseOnMessageReceived; + BCheckBox* fRaiseUserIsTyping; BCheckBox* fPlaySoundOnMessageReceived; BCheckBox* fMarkUnreadWindow; BCheckBox* fMarkUnreadReplicant;