public override void PreOpen()
        {
            base.PreOpen();

            // normalize priorities first time the tab is opened
            // initialize the map component, making sure we have a valid backup and the map component is injected into the game
            // finally, show a disclaimer the first time the tab is opened.
            if (_first)
            {
                _first = false;
                RebuildWorkTypeDefsList();

                foreach (WorkTypeDef workTypeDef in _workTypeDefs)
                {
                    RebuildWorkGiverDefsList(workTypeDef);
                }

                MapComponent_Priorities.InitMapComponent();

                if (!MapComponent_Priorities.userWarned)
                {
                    MapComponent_Priorities.userWarned = true;
                    Find.WindowStack.Add(new Dialog_Message("Fluffy.WorkPrioritiesDisclaimer".Translate(), "Fluffy.WorkPrioritiesDisclaimerTitle".Translate()));
                    Find.WindowStack.WindowOfType <Dialog_Message>().layer = WindowLayer.Super;
                }
            }
        }
        public override void DoWindowContents(Rect inRect)
        {
            // Close if work tab closed
            if (Find.WindowStack.WindowOfType <MainTabWindow_Work>() == null)
            {
                Find.WindowStack.TryRemove(this);
            }

            float areaHeight = (inRect.height - 50f - _margin) / 2f;

            // the space reserved on the UI
            Rect headerRect        = new Rect(inRect.xMin, _margin, inRect.width, 50f - _margin);
            Rect workTypeListRect  = new Rect(inRect.xMin, 50f, inRect.width, areaHeight);
            Rect workGiverListRect = new Rect(inRect.xMin, workTypeListRect.yMax + _margin, inRect.width, areaHeight);

            // leave room for buttons
            workGiverListRect.width -= _buttonSize.x + _margin;
            workTypeListRect.width  -= _buttonSize.x + _margin;

            // button areas
            Rect workTypeSortRect  = new Rect(workTypeListRect.xMax + _margin, workTypeListRect.yMin, _buttonSize.x, areaHeight);
            Rect workGiverSortRect = new Rect(workGiverListRect.xMax + _margin, workGiverListRect.yMin, _buttonSize.x, areaHeight);
            Rect resetRect         = new Rect(inRect.width - 27f, 13f - _margin / 2, 24f, 24f);

            // draw backgrounds
            Widgets.DrawMenuSection(workTypeListRect);
            Widgets.DrawMenuSection(workGiverListRect);

            // leave a tiny margin around the scrollbar if necessary
            if (_workTypeListHeight > workTypeListRect.height)
            {
                workTypeListRect.xMax -= 2f;
            }
            if (_workGiverListHeight > workGiverListRect.height)
            {
                workGiverListRect.xMax -= 2f;
            }

            // the canvas for the (scrollable) lists
            Rect workTypeListContent  = new Rect(workTypeListRect.AtZero());
            Rect workGiverListContent = new Rect(workGiverListRect.AtZero());

            // set height to calculated height after first (every) pass.
            workTypeListContent.height  = _workTypeListHeight;
            workGiverListContent.height = _workGiverListHeight;

            // leave room for scrollbar if necessary.
            workTypeListContent.width  -= _workTypeListHeight > workTypeListRect.height ? 16f : 0f;
            workGiverListContent.width -= _workGiverListHeight > workGiverListRect.height ? 16f : 0f;

            // header
            Text.Font = GameFont.Medium;
            Widgets.Label(headerRect, "Fluffy.WorkPrioritiesDetails".Translate());
            Text.Font = GameFont.Small;

            // reset button
            TooltipHandler.TipRegion(resetRect, "Fluffy.ResetPriorities".Translate());
            if (Widgets.ImageButton(resetRect, ResetButton))
            {
                MapComponent_Priorities.ResetPriorities();
                RebuildWorkTypeDefsList();
            }

            // worktype lister
            GUI.BeginGroup(workTypeListRect);
            Widgets.BeginScrollView(workTypeListRect.AtZero(), ref _workTypeScrollPosition, workTypeListContent);

            // keep track of position
            Vector2 cur = Vector2.zero;

            // draw the listings
            foreach (WorkTypeDef workType in WorkTypeDefs)
            {
                // move with selected when reordering
                if (_workTypeMoved && workType == _selectedWorkTypeDef)
                {
                    _workTypeScrollPosition.y = cur.y - 2 * _entryHeight;
                    _workTypeMoved            = false;
                }
                if (DrawEntry(ref cur, workTypeListContent, workType == _selectedWorkTypeDef, workType))
                {
                    _selectedWorkTypeDef  = workType;
                    _selectedWorkGiverDef = null;
                }
            }

            // set the actual height after having drawn everything
            _workTypeListHeight = cur.y;

            Widgets.EndScrollView();
            GUI.EndGroup();

            // draw buttons
            DrawSortButtons(workTypeSortRect, _selectedWorkTypeDef != null, _selectedWorkTypeDef);


            // START WORKGIVERS
            if (_selectedWorkTypeDef != null)
            {
                // workgiver lister
                GUI.BeginGroup(workGiverListRect);
                Widgets.BeginScrollView(workGiverListRect.AtZero(), ref _workGiverScrollPosition, workGiverListContent);

                // keep track of position
                cur = Vector2.zero;

                // draw the listings
                foreach (WorkGiverDef workGiver in _selectedWorkTypeDef.workGiversByPriority)
                {
                    // move with selected when reordering
                    if (_workGiverMoved && workGiver == _selectedWorkGiverDef)
                    {
                        _workGiverScrollPosition.y = cur.y - 2 * _entryHeight;
                        _workGiverMoved            = false;
                    }
                    if (DrawEntry(ref cur, workGiverListContent, workGiver == _selectedWorkGiverDef, _selectedWorkTypeDef, workGiver))
                    {
                        _selectedWorkGiverDef = workGiver;
                    }
                }

                _workGiverListHeight = cur.y;

                Widgets.EndScrollView();
                GUI.EndGroup();

                // draw buttons
                DrawSortButtons(workGiverSortRect, _selectedWorkGiverDef != null, _selectedWorkTypeDef, _selectedWorkGiverDef);
            }
        }