From 7a5b6c55a2f750986ede116b8f09bda2588de3a9 Mon Sep 17 00:00:00 2001
From: Alexander Golubev <fatzer2@gmail.com>
Date: Thu, 8 Jan 2026 18:17:37 +0300
Subject: twin: refactor Client::updateOpacity() and associated stuff

* deduplicate big chunk of code in updateOpacity()
* move logic to get desired opacity according to rules into a dedicated
  function (defaultOpacity()).
* Replace floating point arithmetics with pure integer one in function
  associated to converting to/from percents.

Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
---
 twin/client.cpp      | 203 +++++++++++++++++----------------------------------
 twin/client.h        |   3 +
 twin/useractions.cpp |  14 ++--
 twin/workspace.cpp   |  13 +++-
 twin/workspace.h     |   2 +
 5 files changed, 90 insertions(+), 145 deletions(-)

diff --git a/twin/client.cpp b/twin/client.cpp
index b5af00d4f..050d1e965 100644
--- a/twin/client.cpp
+++ b/twin/client.cpp
@@ -2801,9 +2801,9 @@ void Client::setOpacity(bool translucent, uint opacity)
         return; // xcompmgr does not like non solid desktops and the user could set it accidently by mouse scrolling
 //     tqWarning("setting opacity for %d",tqt_xdisplay());
     //rule out activated translulcency with 100% opacity
-    if (!translucent || opacity ==  0xFFFFFFFF)
+    if (!translucent || opacity == Opacity::Opaque)
         {
-        opacity_ = 0xFFFFFFFF;
+        opacity_ = Opacity::Opaque;
         XDeleteProperty (tqt_xdisplay(), frameId(), atoms->net_wm_window_opacity);
         XDeleteProperty (tqt_xdisplay(), window(), atoms->net_wm_window_opacity); // ??? frameId() is necessary for visible changes, window() is the winId() that would be set by apps - we set both to be sure the app knows what's currently displayd
         }
@@ -2825,161 +2825,90 @@ void Client::setShadowSize(uint shadowSize)
     XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_shadow, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &data, 1L);
     }
 
-void Client::updateOpacity()
-// extra syncscreen flag allows to avoid double syncs when active state changes (as it will usually change for two windows)
+uint Client::defaultOpacity()
     {
-    if (!(isNormalWindow() || isDialog() || isUtility() )|| custom_opacity)
-        return;
     if (isActive())
         {
         if( ruleOpacityActive() )
-            setOpacity(rule_opacity_active < 0xFFFFFFFF, rule_opacity_active);
+            return rule_opacity_active;
         else
-            setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
-        if (isBMP())
-        // beep-media-player, only undecorated windows (gtk2 xmms, xmms doesn't work with compmgr at all - s.e.p. :P )
-            {
-            ClientList tmpGroupMembers = group()->members();
-            ClientList activeGroupMembers;
-            activeGroupMembers.append(this);
-            tmpGroupMembers.remove(this);
-            ClientList::Iterator it = tmpGroupMembers.begin();
-            while (it != tmpGroupMembers.end())
-            // search for next attached and not activated client and repeat if found
-                {
-                if ((*it) != this && (*it)->isBMP())
-                // potential "to activate" client found
-                    {
-//                     tqWarning("client found");
-                    if ((*it)->touches(this)) // first test, if the new client touches the just activated one
-                        {
-//                         tqWarning("found client touches me");
-                        if( ruleOpacityActive() )
-                            (*it)->setOpacity(rule_opacity_active < 0xFFFFFFFF, rule_opacity_active);
-                        else
-                            (*it)->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
-//                         tqWarning("activated, search restarted (1)");
-                        (*it)->setShadowSize(options->activeWindowShadowSize);
-                        activeGroupMembers.append(*it);
-                        tmpGroupMembers.remove(it);
-                        it = tmpGroupMembers.begin(); // restart, search next client
-                        continue;
-                        }
-                    else
-                        { // pot. client does not touch c, so we have to search if it touches some other activated client
-                        bool found = false;
-                        for( ClientList::ConstIterator it2 = activeGroupMembers.begin(); it2 != activeGroupMembers.end(); it2++ )
-                            {
-                            if ((*it2) != this && (*it2) != (*it) && (*it)->touches(*it2))
-                                {
-//                                 tqWarning("found client touches other active client");
-                                if( ruleOpacityActive() )
-                                    (*it)->setOpacity(rule_opacity_active < 0xFFFFFFFF, rule_opacity_active);
-                                else
-                                    (*it)->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
-                                (*it)->setShadowSize(options->activeWindowShadowSize);
-                                activeGroupMembers.append(*it);
-                                tmpGroupMembers.remove(it);
-                                it = tmpGroupMembers.begin(); // reset potential client search
-                                found = true;
-//                                 tqWarning("activated, search restarted (2)");
-                                break; // skip this loop
-                                }
-                            }
-                        if (found) continue;
-                        }
-                    }
-                    it++;
-                }
-            }
-        else if (isNormalWindow())
-        // activate dependend minor windows as well
-            {
-            for( ClientList::ConstIterator it = group()->members().begin(); it != group()->members().end(); it++ )
-                if ((*it)->isDialog() || (*it)->isUtility())
-                    {
-                    if( (*it)->ruleOpacityActive() )
-                        (*it)->setOpacity((*it)->ruleOpacityActive() < 0xFFFFFFFF, (*it)->ruleOpacityActive());
-                    else
-                        (*it)->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
-                    }
-            }
+            return options->translucentActiveWindows ? options->activeWindowOpacity : Opacity::Opaque;
         }
     else
         {
         if( ruleOpacityInactive() )
-            setOpacity(rule_opacity_inactive < 0xFFFFFFFF, rule_opacity_inactive);
+            return rule_opacity_inactive;
         else
-            setOpacity(options->translucentInactiveWindows && !(keepAbove() && options->keepAboveAsActive),
-                    options->inactiveWindowOpacity);
-        // deactivate dependend minor windows as well
-        if (isBMP())
-        // beep-media-player, only undecorated windows (gtk2 xmms, xmms doesn't work with compmgr at all - s.e.p. :P )
+            return options->translucentInactiveWindows && !(keepAbove() && options->keepAboveAsActive) ?
+                      options->inactiveWindowOpacity
+                    : Opacity::Opaque;
+        }
+    }
+
+void Client::updateOpacity()
+// extra syncscreen flag allows to avoid double syncs when active state changes (as it will usually change for two windows)
+    {
+    if (!(isNormalWindow() || isDialog() || isUtility() )|| custom_opacity)
+        return;
+    uint opacity = defaultOpacity();
+    setOpacity(true, opacity);
+
+    if (isBMP())
+    // beep-media-player, only undecorated windows (gtk2 xmms, xmms doesn't work with compmgr at all - s.e.p. :P )
+        {
+        ClientList tmpGroupMembers = group()->members();
+        ClientList groupMembers;
+        groupMembers.append(this);
+        tmpGroupMembers.remove(this);
+        ClientList::Iterator it = tmpGroupMembers.begin();
+        while (it != tmpGroupMembers.end())
+        // search for next attached and not activated client and repeat if found
             {
-            ClientList tmpGroupMembers = group()->members();
-            ClientList inactiveGroupMembers;
-            inactiveGroupMembers.append(this);
-            tmpGroupMembers.remove(this);
-            ClientList::Iterator it = tmpGroupMembers.begin();
-            while ( it != tmpGroupMembers.end() )
-            // search for next attached and not activated client and repeat if found
+            if ((*it) != this && (*it)->isBMP())
+            // potential "to activate" client found
                 {
-                if ((*it) != this && (*it)->isBMP())
-                // potential "to activate" client found
-                    {
 //                     tqWarning("client found");
-                    if ((*it)->touches(this)) // first test, if the new client touches the just activated one
-                        {
+                if ((*it)->touches(this)) // first test, if the new client touches the just activated one
+                    {
 //                         tqWarning("found client touches me");
-                        if( (*it)->ruleOpacityInactive() )
-                            (*it)->setOpacity((*it)->ruleOpacityInactive() < 0xFFFFFFFF, (*it)->ruleOpacityInactive());
-                        else
-                            (*it)->setOpacity(options->translucentInactiveWindows && !((*it)->keepAbove() && options->keepAboveAsActive), options->inactiveWindowOpacity);
-                        (*it)->setShadowSize(options->inactiveWindowShadowSize);
-//                         tqWarning("deactivated, search restarted (1)");
-                        inactiveGroupMembers.append(*it);
-                        tmpGroupMembers.remove(it);
-                        it = tmpGroupMembers.begin(); // restart, search next client
-                        continue;
-                        }
-                    else // pot. client does not touch c, so we have to search if it touches some other activated client
+                    (*it)->setOpacity(true, opacity);
+//                         tqWarning("(de)activated, search restarted (1)");
+                    (*it)->setShadowSize(options->activeWindowShadowSize);
+                    groupMembers.append(*it);
+                    tmpGroupMembers.remove(it);
+                    it = tmpGroupMembers.begin(); // restart, search next client
+                    continue;
+                    }
+                else
+                    { // pot. client does not touch c, so we have to search if it touches some other activated client
+                    bool found = false;
+                    for( ClientList::ConstIterator it2 = groupMembers.begin(); it2 != groupMembers.end(); it2++ )
                         {
-                        bool found = false;
-                        for( ClientList::ConstIterator it2 = inactiveGroupMembers.begin(); it2 != inactiveGroupMembers.end(); it2++ )
+                        if ((*it2) != this && (*it2) != (*it) && (*it)->touches(*it2))
                             {
-                            if ((*it2) != this && (*it2) != (*it) && (*it)->touches(*it2))
-                                {
-//                                 tqWarning("found client touches other inactive client");
-                                if( (*it)->ruleOpacityInactive() )
-                                    (*it)->setOpacity((*it)->ruleOpacityInactive() < 0xFFFFFFFF, (*it)->ruleOpacityInactive());
-                                else
-                                    (*it)->setOpacity(options->translucentInactiveWindows && !((*it)->keepAbove() && options->keepAboveAsActive), options->inactiveWindowOpacity);
-                                (*it)->setShadowSize(options->inactiveWindowShadowSize);
-//                                 tqWarning("deactivated, search restarted (2)");
-                                inactiveGroupMembers.append(*it);
-                                tmpGroupMembers.remove(it);
-                                it = tmpGroupMembers.begin(); // reset potential client search
-                                found = true;
-                                break; // skip this loop
-                                }
+//                                 tqWarning("found client touches other active client");
+                            (*it)->setOpacity(true, opacity);
+                            (*it)->setShadowSize(isActive() ? options->activeWindowShadowSize : options->inactiveWindowShadowSize);
+                            groupMembers.append(*it);
+                            tmpGroupMembers.remove(it);
+                            it = tmpGroupMembers.begin(); // reset potential client search
+                            found = true;
+//                                 tqWarning("(de)activated, search restarted (2)");
+                            break; // skip this loop
                             }
-                            if (found) continue;
                         }
+                    if (found) continue;
                     }
-                    it++;
                 }
+                it++;
             }
-        else if (isNormalWindow())
-            {
-            for( ClientList::ConstIterator it = group()->members().begin(); it != group()->members().end(); it++ )
-                if ((*it)->isUtility()) //don't deactivate dialogs...
-                    {
-                    if( (*it)->ruleOpacityInactive() )
-                        (*it)->setOpacity((*it)->ruleOpacityInactive() < 0xFFFFFFFF, (*it)->ruleOpacityInactive());
-                    else
-                        (*it)->setOpacity(options->translucentInactiveWindows && !((*it)->keepAbove() && options->keepAboveAsActive), options->inactiveWindowOpacity);
-                    }
-            }
+        }
+    else if (isNormalWindow())
+    // activate/deactivate dependend minor windows as well
+        {
+        for( ClientList::ConstIterator it = group()->members().begin(); it != group()->members().end(); it++ )
+            if ((*it)->isUtility() || ((*it)->isDialog() && isActive() )) // note: don't deactivate dialogs...
+                (*it)->setOpacity(true, opacity);
         }
     }
 
@@ -3034,7 +2963,7 @@ uint Client::opacity()
 
 int Client::opacityPercentage()
     {
-    return int(100*((double)opacity_/0xffffffff));
+    return opacity_ / ( 0xffffffff / 100 );
     }
 
 bool Client::touches(const Client* c)
diff --git a/twin/client.h b/twin/client.h
index 8417da653..4629dfb37 100644
--- a/twin/client.h
+++ b/twin/client.h
@@ -324,11 +324,14 @@ class Client : public TQObject, public KDecorationDefines
 
     // some helpful constants for setOpacity() and related functions
         struct Opacity { enum : uint {
+            Transparent = 0,          //< Completely transparent window
+            MouseStep   = 0xCCCCCCC,  //< A single step when changing opacity with mouse wheel
             Opaque      = 0xFFFFFFFF  //< Fully opaque window
         }; };
 
         void setOpacity(bool translucent, uint opacity = 0);
         void setShadowSize(uint shadowSize);
+        uint defaultOpacity();
         void updateOpacity();
         void updateShadowSize();
         bool hasCustomOpacity(){return custom_opacity;}
diff --git a/twin/useractions.cpp b/twin/useractions.cpp
index fe60c4d04..c573e8214 100644
--- a/twin/useractions.cpp
+++ b/twin/useractions.cpp
@@ -146,8 +146,8 @@ TQPopupMenu* Workspace::clientPopup()
 void Workspace::setPopupClientOpacity(int value)
     {
     active_popup_client->setCustomOpacityFlag(true);
-    value = 100 - value;
-    value<100?active_popup_client->setOpacity(true, (uint)((value/100.0)*0xffffffff)):active_popup_client->setOpacity(false,0xffffffff);
+    int opacityPercent = 100 - value;
+    active_popup_client->setOpacity(true, percentToUint(opacityPercent));
     }
 
 void Workspace::setTransButtonText(int value)
@@ -648,16 +648,16 @@ bool Client::performMouseCommand( Options::MouseCommand command, TQPoint globalP
             workspace()->windowToNextDesktop( this );
             break;
         case Options::MouseOpacityMore:
-            if (opacity_ < 0xFFFFFFFF)
+            if (opacity_ < Opacity::Opaque)
                 {
-                if (opacity_ < 0xF3333333)
+                if (opacity_ < Opacity::Opaque - Opacity::MouseStep)
                     {
-                    setOpacity(true, opacity_ + 0xCCCCCCC);
+                    setOpacity(true, opacity_ + Opacity::MouseStep);
                     custom_opacity = true;
                     }
                 else
                     {
-                    setOpacity(false, 0xFFFFFFFF);
+                    setOpacity(false, Opacity::Opaque);
                     custom_opacity = false;
                     }
                 }
@@ -665,7 +665,7 @@ bool Client::performMouseCommand( Options::MouseCommand command, TQPoint globalP
         case Options::MouseOpacityLess:
             if (opacity_ > 0)
                 {
-                setOpacity(true, (opacity_ > 0xCCCCCCC) ? opacity_ - 0xCCCCCCC : 0);
+                setOpacity(true, (opacity_ > Opacity::MouseStep) ? opacity_ - Opacity::MouseStep : Opacity::Transparent);
                 custom_opacity = true;
                 }
             break;
diff --git a/twin/workspace.cpp b/twin/workspace.cpp
index f2f234b54..6c7c0cf7f 100644
--- a/twin/workspace.cpp
+++ b/twin/workspace.cpp
@@ -3084,13 +3084,24 @@ void Workspace::handleKompmgrOutput( TDEProcess* , char *buffer, int buflen)
         }
 }
 
+uint Workspace::percentToUint(int percent) {
+    if(percent < 0) {
+        return 0;
+    } else if (percent<100) {
+        // the same as "percent / 100.0 * 0xffffffff" but avoids FP arithmetics and overflows
+        return (0xffffffff/100) * (uint) percent + (0xffffffff % 100) * percent / 100;
+    } else {
+        return 0xffffffff;
+    }
+}
+
 void Workspace::setOpacity(unsigned long winId, unsigned int opacityPercent)
 {
     if (opacityPercent > 100) opacityPercent = 100;
     for( ClientList::ConstIterator it = stackingOrder().begin(); it != stackingOrder().end(); it++ )
         if (winId == (*it)->window())
             {
-            (*it)->setOpacity(opacityPercent < 100, (unsigned int)((opacityPercent/100.0)*0xFFFFFFFF));
+            (*it)->setOpacity(opacityPercent < 100, percentToUint(opacityPercent));
             return;
             }
 }
diff --git a/twin/workspace.h b/twin/workspace.h
index 5f1d71025..023854398 100644
--- a/twin/workspace.h
+++ b/twin/workspace.h
@@ -538,6 +538,8 @@ class Workspace : public TQObject, public KWinInterface, public KDecorationDefin
         static const char* windowTypeToTxt( NET::WindowType type );
         static NET::WindowType txtToWindowType( const char* txt );
         static bool sessionInfoWindowTypeMatch( Client* c, SessionInfo* info );
+        /// A small helper which proportionally converts value in range 0-100 into 0-0xffffffff
+        static uint percentToUint(int percent);
 
         Client* active_client;
         Client* last_active_client;
-- 
cgit v1.2.3

