Repository: vscode
Issue: Cell toolbars are not correct on newly opened notebook
Labels: bug,notebook-celltoolbar

Type: <b>Bug</b>

In a Jupyter notebook, toolbars are out of sync for some cells when the notebook is first opened.

## Reproduction

### Case 1

Create a Jupyter notebook with Python as the back end. (I don't know if this bug is Python-specific, but Python is what I'm using.) You will get an empty Python cell "for free."

Hover the cursor above that cell. Click the "+ Markdown" button.

Notice that the toobar contains the Python buttons: "Run by Line", "Execute Above Cells", "Execute Cell and Below, "..." and the trash can.

Now, here is the fun part. Switch to another app window, then switch back. Voila! The buttons are  now the Markdown buttons: Check mark, "Split Cell", "..." and the trash can.

Once you've switched to another app window like this, the problem will not recur in that same notebook editor. To reproduce the following, you'll need
a setup so you can see these instructions _without_ switching windows.

### Case 2

Close the notebook you just created. Open it again. The Markdown cell will be active, only the "..." and trashcan buttons appear in the toolar. 

Switch app windows. The Markdown buttons appear.

### Case 3

Close the test notebook and open it again. Since you did not click the check mark on the Markdown cells, the reopened notebook should still have that cell in edit mode, so we'd expect the Mardown edit buttons.

Hover the cursor before your Python cell (you should only have a Markdown cell and a Python cell at this point.)

Click the "+ Markdown" button. Your new Markdown cell will have the Python cell buttons.

Again, switching windows brings up the proper Markdown buttons.

### Case 4

Close the notebook and reopen it again. Click on each of your three cells. You will get a toolbar with just the "..." and Toolbar buttons. 

This part gets weird. If you click from the bottom up, the bottom two
cells (Markdown at the bottom, Python in the middle) will have the generic buttons, but the top cell has the Markdown buttons. Sometimes all three cells have the generic buttons.

Now click around some more. At some point, you no longer get the generic buttons but instead start getting the proper buttons for the cell type.

Again, if you switch windows, you will always get the proper buttons after you switch back to your notebook.

### Case 5

One final case. Close your notebook and reopen it again.

Click in the last cell (Markdown) and type "something".

Click the checkmark to save the changes.

Hover the cursor below the Markdown cell. Click the "+ Markdown" button.

Notice that you get a new Markdown cell, in edit mode. You can type text. But the toolbar contains only the "..." and Trashcan icons.

Switch to another app window and back to VS Code. The toolbar now contains the check mark.

## Expected Behavior

It should go without saying, but the expected behavior is that the toolbar is consistent: I shouldn't have to switch app windows to get the proper toolbar.

The workaround is to either switch app windows, or click onto another cell and back again.

## Comment

It seems that something is off with the code that updates toolbars based on events. That code seems to be missing an event early on, but receives the correct event on loss of focus. Once whatever the problem is is adjusted once, that notebook stays in "working" mode thereafter.

## Details

Extension version: 1.0.17
VS Code version: Code 1.89.0 (b58957e67ee1e712cebf466b995adf4c5307b2bd, 2024-05-01T02:08:25.066Z)
OS version: Linux x64 5.15.0-101-generic
Modes:

<details>
<summary>System Info</summary>

|Item|Value|
|---|---|
|CPUs|Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz (8 x 1600)|
|GPU Status|2d_canvas: enabled<br>canvas_oop_rasterization: disabled_off<br>direct_rendering_display_compositor: disabled_off_ok<br>gpu_compositing: enabled<br>multiple_raster_threads: enabled_on<br>opengl: enabled_on<br>rasterization: enabled<br>raw_draw: disabled_off_ok<br>skia_graphite: disabled_off<br>video_decode: enabled<br>video_encode: disabled_software<br>vulkan: disabled_off<br>webgl: enabled<br>webgl2: enabled<br>webgpu: disabled_off|
|Load (avg)|1, 1, 2|
|Memory (System)|31.31GB (7.80GB free)|
|Process Argv|--crash-reporter-id 5e232e7b-355a-468c-ac4a-02c300f898bd|
|Screen Reader|no|
|VM|0%|
|DESKTOP_SESSION|cinnamon|
|XDG_CURRENT_DESKTOP|X-Cinnamon|
|XDG_SESSION_DESKTOP|cinnamon|
|XDG_SESSION_TYPE|x11|
</details><details>
<summary>A/B Experiments</summary>

```
vsliv368cf:30146710
vspor879:30202332
vspor708:30202333
vspor363:30204092
vstes627:30244334
vscorecescf:30445987
vscod805:30301674
binariesv615:30325510
vsaa593:30376534
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
a9j8j154:30646983
962ge761:30959799
pythongtdpath:30769146
welcomedialog:30910333
pythonidxpt:30866567
pythonnoceb:30805159
asynctok:30898717
pythontestfixt:30902429
pythonregdiag2:30936856
pyreplss1:30897532
pythonmypyd1:30879173
pythoncet0:30885854
h48ei257:31000450
pythontbext0:30879054
accentitlementsc:30995553
dsvsc016:30899300
dsvsc017:30899301
dsvsc018:30899302
cppperfnew:31000557
ccp2r3:30993541
dsvsc020:30976470
pythonait:31006305
dsvsc021:30996838
g316j359:31013175
pythoncenvpt:31022790
dwnewjupytercf:31035177

```

</details>

<!-- generated by issue reporter -->